調べたことやコード変更した内容について下記のようにコメントを入れることにしています。

In [1]:
#【コメント】コメントです

Googleドライブのマウント

In [ ]:
from google.colab import drive
drive.mount('/content/drive')
In [3]:
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

%matplotlib inline
In [4]:
#seaborn設定
sns.set()
#背景変更
sns.set_style("darkgrid", {'grid.linestyle': '--'})
#大きさ(スケール変更)
sns.set_context("paper")
In [5]:
n=100

def true_func(x):
    z = 1-48*x+218*x**2-315*x**3+145*x**4
    return z 

def linear_func(x):
    z = x
    return z 

【コメント】

真の関数:xの4次関数

In [6]:
# 真の関数からノイズを伴うデータを生成

# 真の関数からデータ生成
data = np.random.rand(n).astype(np.float32)
  #【コメント】乱数でデータのx座標を生成
  #【コメント】一様分布で0.0-1.0の乱数
data = np.sort(data)
  #【コメント】ここでソートする必要性が謎
target = true_func(data)
  #【コメント】対応するy座標の生成

#  ノイズを加える
noise = 0.5 * np.random.randn(n) 
target = target  + noise
  #【コメント】標準正規分布に従う乱数-0.5~+0.5

# ノイズ付きデータを描画

plt.scatter(data, target)

plt.title('NonLinear Regression')
plt.legend(loc=2)
No handles with labels found to put in legend.
Out[6]:
<matplotlib.legend.Legend at 0x7f8010b13f50>
In [7]:
from sklearn.linear_model import LinearRegression

clf = LinearRegression()
  #【コメント】線形回帰モデル
  #【コメント】clfはclassifier(分類器)の略らしい
data = data.reshape(-1,1)
  #【コメント】縦ベクトルに変換
target = target.reshape(-1,1)
  #【コメント】縦ベクトルに変換
clf.fit(data, target)


p_lin = clf.predict(data)


plt.scatter(data, target, label='data')
plt.plot(data, p_lin, color='darkorange', marker='', linestyle='-', linewidth=1, markersize=6, label='linear regression')
plt.legend()
print(clf.score(data, target))
  #【コメント】決定係数を出力⇒0.251
0.20945642459375569
In [8]:
from sklearn.kernel_ridge import KernelRidge

clf = KernelRidge(alpha=0.0002, kernel='rbf')
  #【コメント】カーネルリッジ回帰モデル
  #【コメント】alphaは正則化係数
  #【コメント】kernelは基底関数?
clf.fit(data, target)

p_kridge = clf.predict(data)

plt.scatter(data, target, color='blue', label='data')

plt.plot(data, p_kridge, color='orange', linestyle='-', linewidth=3, markersize=6, label='kernel ridge')
plt.legend()
#plt.plot(data, p, color='orange', marker='o', linestyle='-', linewidth=1, markersize=6)
print(clf.score(data, target))
  #【コメント】決定係数を出力⇒0.857!
0.8092282163027809
In [9]:
#Ridge

from sklearn.metrics.pairwise import rbf_kernel
from sklearn.linear_model import Ridge

kx = rbf_kernel(X=data, Y=data, gamma=50)
#KX = rbf_kernel(X, x)
  #【コメント】(100,100)の配列
  #【コメント】説明変数を関数で変換(これをカーネルという?)

#clf = LinearRegression()
clf = Ridge(alpha=30)
  #【コメント】リッジ回帰モデル
clf.fit(kx, target)

p_ridge = clf.predict(kx)

plt.scatter(data, target,label='data')
for i in range(len(kx)):
    plt.plot(data, kx[i], color='black', linestyle='-', linewidth=1, markersize=3, label='rbf', alpha=0.2)

#plt.plot(data, p, color='green', marker='o', linestyle='-', linewidth=0.1, markersize=3)
plt.plot(data, p_ridge, color='green', linestyle='-', linewidth=1, markersize=3,label='ridge regression')
#plt.legend()

print(clf.score(kx, target))
  #【コメント】決定係数を出力⇒0.817!
0.7365226010875578
In [10]:
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import Pipeline
In [11]:
#PolynomialFeatures(degree=1)

deg = [1,2,3,4,5,6,7,8,9,10]
for d in deg:
    regr = Pipeline([
        ('poly', PolynomialFeatures(degree=d)),
        ('linear', LinearRegression())
    ])
      #【コメント】説明変数をd次までのべき乗データを用意したのち線形回帰⇒多項式回帰
    regr.fit(data, target)
    # make predictions
    p_poly = regr.predict(data)
    # plot regression result
    plt.scatter(data, target, label='data')
    plt.plot(data, p_poly, label='polynomial of degree %d' % (d))
In [12]:
#Lasso

from sklearn.metrics.pairwise import rbf_kernel
from sklearn.linear_model import Lasso

kx = rbf_kernel(X=data, Y=data, gamma=5)
#KX = rbf_kernel(X, x)

#lasso_clf = LinearRegression()
lasso_clf = Lasso(alpha=10000, max_iter=1000)
  #【コメント】ラッソ回帰
  #【コメント】正則化が効きすぎているためモデルの表現力が死んでいる?
lasso_clf.fit(kx, target)

p_lasso = lasso_clf.predict(kx)

plt.scatter(data, target)

#plt.plot(data, p, color='green', marker='o', linestyle='-', linewidth=0.1, markersize=3)
plt.plot(data, p_lasso, color='green', linestyle='-', linewidth=3, markersize=3)

print(lasso_clf.score(kx, target))
-2.220446049250313e-16
In [13]:
from sklearn import model_selection, preprocessing, linear_model, svm

# SVR-rbf
clf_svr = svm.SVR(kernel='rbf', C=1e3, gamma=0.1, epsilon=0.1)
    #【コメント】サポートベクター回帰
clf_svr.fit(data, target)
y_rbf = clf_svr.fit(data, target).predict(data)
 
# plot

plt.scatter(data, target, color='darkorange', label='data')
plt.plot(data, y_rbf, color='red', label='Support Vector Regression (RBF)')
plt.legend()
plt.show()

print(clf.score(kx, target))
  #【コメント】決定係数を出力⇒0.387
  #【コメント】ハイパーパラメータの調整が必要そう
/usr/local/lib/python3.7/dist-packages/sklearn/utils/validation.py:985: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
  y = column_or_1d(y, warn=True)
/usr/local/lib/python3.7/dist-packages/sklearn/utils/validation.py:985: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
  y = column_or_1d(y, warn=True)
0.24767615991134684
In [14]:
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(data, target, test_size=0.1, random_state=0)

以下では,Googleドライブのマイドライブ直下にstudy_ai_mlフォルダを置くことを仮定しています.必要に応じて,パスを変更してください。

In [15]:
from keras.callbacks import EarlyStopping, TensorBoard, ModelCheckpoint

cb_cp = ModelCheckpoint('/content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.{epoch:02d}-{val_loss:.2f}.hdf5', verbose=1, save_weights_only=True)
cb_tf  = TensorBoard(log_dir='/content/drive/My Drive/study_ai_ml_google/skl_ml/out/tensorBoard', histogram_freq=0)
  #【コメント】エラーが出るのでパス名を編集  study_ai_ml → study_ai_ml_google
In [16]:
  #【コメント】ディープラーニングなので現段階では実行のみとしておく
def relu_reg_model():
    model = Sequential()
    model.add(Dense(10, input_dim=1, activation='relu'))
    model.add(Dense(1000, activation='relu'))
    model.add(Dense(1000, activation='relu'))
    model.add(Dense(1000, activation='relu'))
    model.add(Dense(1000, activation='relu'))
    model.add(Dense(1000, activation='relu'))
    model.add(Dense(1000, activation='relu'))
    model.add(Dense(1000, activation='relu'))
    model.add(Dense(1000, activation='linear'))
#     model.add(Dense(100, activation='relu'))
#     model.add(Dense(100, activation='relu'))
#     model.add(Dense(100, activation='relu'))
#     model.add(Dense(100, activation='relu'))
    model.add(Dense(1))

    model.compile(loss='mean_squared_error', optimizer='adam')
    return model
In [17]:
from keras.models import Sequential
from keras.layers import Input, Dense, Dropout, BatchNormalization
from keras.wrappers.scikit_learn import KerasRegressor

# use data split and fit to run the model
estimator = KerasRegressor(build_fn=relu_reg_model, epochs=100, batch_size=5, verbose=1)

history = estimator.fit(x_train, y_train, callbacks=[cb_cp, cb_tf], validation_data=(x_test, y_test))
/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:6: DeprecationWarning: KerasRegressor is deprecated, use Sci-Keras (https://github.com/adriangb/scikeras) instead.
  
Epoch 1/100
18/18 [==============================] - ETA: 0s - loss: 1.1900
Epoch 00001: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.01-1.53.hdf5
18/18 [==============================] - 2s 31ms/step - loss: 1.1900 - val_loss: 1.5268
Epoch 2/100
17/18 [===========================>..] - ETA: 0s - loss: 0.8150
Epoch 00002: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.02-1.24.hdf5
18/18 [==============================] - 0s 21ms/step - loss: 0.8266 - val_loss: 1.2377
Epoch 3/100
13/18 [====================>.........] - ETA: 0s - loss: 0.6199
Epoch 00003: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.03-1.38.hdf5
18/18 [==============================] - 0s 20ms/step - loss: 0.6456 - val_loss: 1.3843
Epoch 4/100
14/18 [======================>.......] - ETA: 0s - loss: 0.9698
Epoch 00004: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.04-1.03.hdf5
18/18 [==============================] - 0s 24ms/step - loss: 0.9528 - val_loss: 1.0257
Epoch 5/100
17/18 [===========================>..] - ETA: 0s - loss: 0.6434
Epoch 00005: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.05-0.95.hdf5
18/18 [==============================] - 0s 24ms/step - loss: 0.6265 - val_loss: 0.9492
Epoch 6/100
18/18 [==============================] - ETA: 0s - loss: 0.7867
Epoch 00006: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.06-1.48.hdf5
18/18 [==============================] - 0s 24ms/step - loss: 0.7867 - val_loss: 1.4796
Epoch 7/100
18/18 [==============================] - ETA: 0s - loss: 0.6703
Epoch 00007: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.07-0.81.hdf5
18/18 [==============================] - 0s 23ms/step - loss: 0.6703 - val_loss: 0.8087
Epoch 8/100
18/18 [==============================] - ETA: 0s - loss: 0.5212
Epoch 00008: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.08-0.66.hdf5
18/18 [==============================] - 0s 20ms/step - loss: 0.5212 - val_loss: 0.6630
Epoch 9/100
18/18 [==============================] - ETA: 0s - loss: 0.3950
Epoch 00009: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.09-0.36.hdf5
18/18 [==============================] - 0s 21ms/step - loss: 0.3950 - val_loss: 0.3643
Epoch 10/100
13/18 [====================>.........] - ETA: 0s - loss: 0.4145
Epoch 00010: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.10-0.72.hdf5
18/18 [==============================] - 0s 26ms/step - loss: 0.5179 - val_loss: 0.7167
Epoch 11/100
18/18 [==============================] - ETA: 0s - loss: 0.3889
Epoch 00011: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.11-0.43.hdf5
18/18 [==============================] - 0s 22ms/step - loss: 0.3889 - val_loss: 0.4252
Epoch 12/100
16/18 [=========================>....] - ETA: 0s - loss: 0.3653
Epoch 00012: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.12-0.38.hdf5
18/18 [==============================] - 0s 23ms/step - loss: 0.3525 - val_loss: 0.3771
Epoch 13/100
13/18 [====================>.........] - ETA: 0s - loss: 0.4929
Epoch 00013: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.13-0.51.hdf5
18/18 [==============================] - 0s 19ms/step - loss: 0.4779 - val_loss: 0.5074
Epoch 14/100
13/18 [====================>.........] - ETA: 0s - loss: 0.3560
Epoch 00014: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.14-0.61.hdf5
18/18 [==============================] - 0s 24ms/step - loss: 0.3289 - val_loss: 0.6136
Epoch 15/100
17/18 [===========================>..] - ETA: 0s - loss: 0.7924
Epoch 00015: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.15-0.45.hdf5
18/18 [==============================] - 0s 22ms/step - loss: 0.7784 - val_loss: 0.4493
Epoch 16/100
13/18 [====================>.........] - ETA: 0s - loss: 0.7009
Epoch 00016: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.16-0.77.hdf5
18/18 [==============================] - 0s 20ms/step - loss: 0.7033 - val_loss: 0.7740
Epoch 17/100
18/18 [==============================] - ETA: 0s - loss: 0.8384
Epoch 00017: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.17-1.07.hdf5
18/18 [==============================] - 0s 22ms/step - loss: 0.8384 - val_loss: 1.0673
Epoch 18/100
13/18 [====================>.........] - ETA: 0s - loss: 0.8334
Epoch 00018: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.18-1.16.hdf5
18/18 [==============================] - 0s 20ms/step - loss: 0.7048 - val_loss: 1.1580
Epoch 19/100
16/18 [=========================>....] - ETA: 0s - loss: 0.4798
Epoch 00019: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.19-0.64.hdf5
18/18 [==============================] - 0s 27ms/step - loss: 0.4726 - val_loss: 0.6403
Epoch 20/100
13/18 [====================>.........] - ETA: 0s - loss: 0.4714
Epoch 00020: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.20-0.71.hdf5
18/18 [==============================] - 0s 23ms/step - loss: 0.4565 - val_loss: 0.7110
Epoch 21/100
13/18 [====================>.........] - ETA: 0s - loss: 0.4773
Epoch 00021: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.21-0.44.hdf5
18/18 [==============================] - 0s 21ms/step - loss: 0.4418 - val_loss: 0.4420
Epoch 22/100
17/18 [===========================>..] - ETA: 0s - loss: 0.4409
Epoch 00022: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.22-0.87.hdf5
18/18 [==============================] - 0s 21ms/step - loss: 0.4659 - val_loss: 0.8703
Epoch 23/100
18/18 [==============================] - ETA: 0s - loss: 0.6426
Epoch 00023: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.23-0.74.hdf5
18/18 [==============================] - 1s 37ms/step - loss: 0.6426 - val_loss: 0.7429
Epoch 24/100
18/18 [==============================] - ETA: 0s - loss: 0.4160
Epoch 00024: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.24-0.37.hdf5
18/18 [==============================] - 1s 57ms/step - loss: 0.4160 - val_loss: 0.3718
Epoch 25/100
17/18 [===========================>..] - ETA: 0s - loss: 0.3783
Epoch 00025: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.25-0.39.hdf5
18/18 [==============================] - 1s 67ms/step - loss: 0.3792 - val_loss: 0.3885
Epoch 26/100
15/18 [========================>.....] - ETA: 0s - loss: 0.5803
Epoch 00026: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.26-0.43.hdf5
18/18 [==============================] - 1s 35ms/step - loss: 0.6106 - val_loss: 0.4306
Epoch 27/100
18/18 [==============================] - ETA: 0s - loss: 0.4523
Epoch 00027: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.27-0.90.hdf5
18/18 [==============================] - 1s 36ms/step - loss: 0.4523 - val_loss: 0.8982
Epoch 28/100
16/18 [=========================>....] - ETA: 0s - loss: 0.4958
Epoch 00028: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.28-0.50.hdf5
18/18 [==============================] - 1s 36ms/step - loss: 0.4786 - val_loss: 0.5023
Epoch 29/100
16/18 [=========================>....] - ETA: 0s - loss: 0.4671
Epoch 00029: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.29-0.31.hdf5
18/18 [==============================] - 1s 52ms/step - loss: 0.4439 - val_loss: 0.3102
Epoch 30/100
15/18 [========================>.....] - ETA: 0s - loss: 0.3776
Epoch 00030: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.30-0.54.hdf5
18/18 [==============================] - 1s 41ms/step - loss: 0.3647 - val_loss: 0.5445
Epoch 31/100
13/18 [====================>.........] - ETA: 0s - loss: 0.3739
Epoch 00031: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.31-0.29.hdf5
18/18 [==============================] - 1s 33ms/step - loss: 0.3209 - val_loss: 0.2913
Epoch 32/100
18/18 [==============================] - ETA: 0s - loss: 0.3546
Epoch 00032: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.32-0.42.hdf5
18/18 [==============================] - 0s 21ms/step - loss: 0.3546 - val_loss: 0.4240
Epoch 33/100
13/18 [====================>.........] - ETA: 0s - loss: 0.3164
Epoch 00033: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.33-0.19.hdf5
18/18 [==============================] - 0s 19ms/step - loss: 0.3486 - val_loss: 0.1917
Epoch 34/100
16/18 [=========================>....] - ETA: 0s - loss: 0.3243
Epoch 00034: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.34-0.42.hdf5
18/18 [==============================] - 1s 30ms/step - loss: 0.3027 - val_loss: 0.4154
Epoch 35/100
18/18 [==============================] - ETA: 0s - loss: 0.3110
Epoch 00035: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.35-0.39.hdf5
18/18 [==============================] - 0s 23ms/step - loss: 0.3110 - val_loss: 0.3878
Epoch 36/100
18/18 [==============================] - ETA: 0s - loss: 0.3110
Epoch 00036: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.36-0.37.hdf5
18/18 [==============================] - 0s 21ms/step - loss: 0.3110 - val_loss: 0.3654
Epoch 37/100
14/18 [======================>.......] - ETA: 0s - loss: 0.3690
Epoch 00037: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.37-0.45.hdf5
18/18 [==============================] - 0s 28ms/step - loss: 0.3392 - val_loss: 0.4540
Epoch 38/100
16/18 [=========================>....] - ETA: 0s - loss: 0.4629
Epoch 00038: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.38-0.26.hdf5
18/18 [==============================] - 0s 25ms/step - loss: 0.4207 - val_loss: 0.2602
Epoch 39/100
14/18 [======================>.......] - ETA: 0s - loss: 0.3683
Epoch 00039: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.39-0.78.hdf5
18/18 [==============================] - 1s 30ms/step - loss: 0.4850 - val_loss: 0.7809
Epoch 40/100
13/18 [====================>.........] - ETA: 0s - loss: 0.3301
Epoch 00040: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.40-0.30.hdf5
18/18 [==============================] - 1s 54ms/step - loss: 0.3983 - val_loss: 0.3004
Epoch 41/100
16/18 [=========================>....] - ETA: 0s - loss: 0.4314
Epoch 00041: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.41-0.65.hdf5
18/18 [==============================] - 0s 23ms/step - loss: 0.4153 - val_loss: 0.6548
Epoch 42/100
15/18 [========================>.....] - ETA: 0s - loss: 0.3607
Epoch 00042: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.42-0.31.hdf5
18/18 [==============================] - 0s 25ms/step - loss: 0.3207 - val_loss: 0.3111
Epoch 43/100
13/18 [====================>.........] - ETA: 0s - loss: 0.2404
Epoch 00043: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.43-0.32.hdf5
18/18 [==============================] - 0s 19ms/step - loss: 0.2736 - val_loss: 0.3233
Epoch 44/100
18/18 [==============================] - ETA: 0s - loss: 0.3025
Epoch 00044: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.44-0.54.hdf5
18/18 [==============================] - 0s 26ms/step - loss: 0.3025 - val_loss: 0.5446
Epoch 45/100
15/18 [========================>.....] - ETA: 0s - loss: 0.3832
Epoch 00045: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.45-0.47.hdf5
18/18 [==============================] - 1s 32ms/step - loss: 0.3743 - val_loss: 0.4663
Epoch 46/100
17/18 [===========================>..] - ETA: 0s - loss: 0.3524
Epoch 00046: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.46-0.52.hdf5
18/18 [==============================] - 1s 32ms/step - loss: 0.3434 - val_loss: 0.5177
Epoch 47/100
18/18 [==============================] - ETA: 0s - loss: 0.3639
Epoch 00047: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.47-0.24.hdf5
18/18 [==============================] - 0s 20ms/step - loss: 0.3639 - val_loss: 0.2404
Epoch 48/100
14/18 [======================>.......] - ETA: 0s - loss: 0.3383
Epoch 00048: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.48-0.57.hdf5
18/18 [==============================] - 1s 40ms/step - loss: 0.3417 - val_loss: 0.5672
Epoch 49/100
16/18 [=========================>....] - ETA: 0s - loss: 0.3238
Epoch 00049: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.49-0.47.hdf5
18/18 [==============================] - 1s 38ms/step - loss: 0.3120 - val_loss: 0.4660
Epoch 50/100
15/18 [========================>.....] - ETA: 0s - loss: 0.2961
Epoch 00050: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.50-0.68.hdf5
18/18 [==============================] - 0s 26ms/step - loss: 0.3285 - val_loss: 0.6778
Epoch 51/100
17/18 [===========================>..] - ETA: 0s - loss: 0.3635
Epoch 00051: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.51-0.23.hdf5
18/18 [==============================] - 0s 27ms/step - loss: 0.3665 - val_loss: 0.2252
Epoch 52/100
17/18 [===========================>..] - ETA: 0s - loss: 0.3268
Epoch 00052: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.52-0.74.hdf5
18/18 [==============================] - 1s 34ms/step - loss: 0.3260 - val_loss: 0.7407
Epoch 53/100
17/18 [===========================>..] - ETA: 0s - loss: 0.3351
Epoch 00053: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.53-0.24.hdf5
18/18 [==============================] - 1s 33ms/step - loss: 0.3407 - val_loss: 0.2379
Epoch 54/100
16/18 [=========================>....] - ETA: 0s - loss: 0.3181
Epoch 00054: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.54-0.51.hdf5
18/18 [==============================] - 0s 28ms/step - loss: 0.2983 - val_loss: 0.5104
Epoch 55/100
13/18 [====================>.........] - ETA: 0s - loss: 0.4010
Epoch 00055: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.55-0.61.hdf5
18/18 [==============================] - 1s 29ms/step - loss: 0.5504 - val_loss: 0.6108
Epoch 56/100
18/18 [==============================] - ETA: 0s - loss: 0.8176
Epoch 00056: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.56-0.80.hdf5
18/18 [==============================] - 1s 34ms/step - loss: 0.8176 - val_loss: 0.7950
Epoch 57/100
15/18 [========================>.....] - ETA: 0s - loss: 0.5968
Epoch 00057: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.57-0.61.hdf5
18/18 [==============================] - 1s 31ms/step - loss: 0.5403 - val_loss: 0.6146
Epoch 58/100
17/18 [===========================>..] - ETA: 0s - loss: 0.4033
Epoch 00058: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.58-0.52.hdf5
18/18 [==============================] - 1s 29ms/step - loss: 0.3835 - val_loss: 0.5171
Epoch 59/100
18/18 [==============================] - ETA: 0s - loss: 0.3452
Epoch 00059: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.59-0.24.hdf5
18/18 [==============================] - 0s 22ms/step - loss: 0.3452 - val_loss: 0.2356
Epoch 60/100
13/18 [====================>.........] - ETA: 0s - loss: 0.4566
Epoch 00060: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.60-0.61.hdf5
18/18 [==============================] - 0s 20ms/step - loss: 0.4001 - val_loss: 0.6095
Epoch 61/100
17/18 [===========================>..] - ETA: 0s - loss: 0.3714
Epoch 00061: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.61-0.53.hdf5
18/18 [==============================] - 1s 59ms/step - loss: 0.3613 - val_loss: 0.5255
Epoch 62/100
17/18 [===========================>..] - ETA: 0s - loss: 0.2946
Epoch 00062: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.62-0.35.hdf5
18/18 [==============================] - 0s 28ms/step - loss: 0.3039 - val_loss: 0.3453
Epoch 63/100
18/18 [==============================] - ETA: 0s - loss: 0.2983
Epoch 00063: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.63-0.50.hdf5
18/18 [==============================] - 1s 30ms/step - loss: 0.2983 - val_loss: 0.5044
Epoch 64/100
18/18 [==============================] - ETA: 0s - loss: 0.4207
Epoch 00064: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.64-0.59.hdf5
18/18 [==============================] - 0s 27ms/step - loss: 0.4207 - val_loss: 0.5918
Epoch 65/100
18/18 [==============================] - ETA: 0s - loss: 0.3359
Epoch 00065: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.65-0.31.hdf5
18/18 [==============================] - 1s 31ms/step - loss: 0.3359 - val_loss: 0.3078
Epoch 66/100
17/18 [===========================>..] - ETA: 0s - loss: 0.2824
Epoch 00066: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.66-0.41.hdf5
18/18 [==============================] - 0s 23ms/step - loss: 0.2856 - val_loss: 0.4055
Epoch 67/100
16/18 [=========================>....] - ETA: 0s - loss: 0.3770
Epoch 00067: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.67-0.61.hdf5
18/18 [==============================] - 0s 27ms/step - loss: 0.3571 - val_loss: 0.6052
Epoch 68/100
18/18 [==============================] - ETA: 0s - loss: 0.3023
Epoch 00068: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.68-0.36.hdf5
18/18 [==============================] - 0s 25ms/step - loss: 0.3023 - val_loss: 0.3573
Epoch 69/100
13/18 [====================>.........] - ETA: 0s - loss: 0.2631
Epoch 00069: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.69-0.28.hdf5
18/18 [==============================] - 0s 25ms/step - loss: 0.2667 - val_loss: 0.2777
Epoch 70/100
15/18 [========================>.....] - ETA: 0s - loss: 0.3151
Epoch 00070: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.70-0.34.hdf5
18/18 [==============================] - 0s 23ms/step - loss: 0.3134 - val_loss: 0.3405
Epoch 71/100
17/18 [===========================>..] - ETA: 0s - loss: 0.3755
Epoch 00071: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.71-0.20.hdf5
18/18 [==============================] - 1s 76ms/step - loss: 0.3711 - val_loss: 0.1980
Epoch 72/100
18/18 [==============================] - ETA: 0s - loss: 0.3521
Epoch 00072: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.72-0.27.hdf5
18/18 [==============================] - 1s 30ms/step - loss: 0.3521 - val_loss: 0.2703
Epoch 73/100
18/18 [==============================] - ETA: 0s - loss: 0.3036
Epoch 00073: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.73-0.20.hdf5
18/18 [==============================] - 0s 28ms/step - loss: 0.3036 - val_loss: 0.1990
Epoch 74/100
18/18 [==============================] - ETA: 0s - loss: 0.4189
Epoch 00074: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.74-0.50.hdf5
18/18 [==============================] - 0s 26ms/step - loss: 0.4189 - val_loss: 0.4952
Epoch 75/100
16/18 [=========================>....] - ETA: 0s - loss: 0.3160
Epoch 00075: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.75-0.44.hdf5
18/18 [==============================] - 1s 29ms/step - loss: 0.2940 - val_loss: 0.4400
Epoch 76/100
16/18 [=========================>....] - ETA: 0s - loss: 0.2638
Epoch 00076: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.76-0.50.hdf5
18/18 [==============================] - 0s 29ms/step - loss: 0.2656 - val_loss: 0.4976
Epoch 77/100
17/18 [===========================>..] - ETA: 0s - loss: 0.2478
Epoch 00077: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.77-0.46.hdf5
18/18 [==============================] - 0s 26ms/step - loss: 0.2969 - val_loss: 0.4555
Epoch 78/100
17/18 [===========================>..] - ETA: 0s - loss: 0.2971
Epoch 00078: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.78-0.15.hdf5
18/18 [==============================] - 0s 20ms/step - loss: 0.3069 - val_loss: 0.1510
Epoch 79/100
16/18 [=========================>....] - ETA: 0s - loss: 0.2697
Epoch 00079: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.79-0.62.hdf5
18/18 [==============================] - 1s 46ms/step - loss: 0.2902 - val_loss: 0.6230
Epoch 80/100
18/18 [==============================] - ETA: 0s - loss: 0.3239
Epoch 00080: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.80-0.51.hdf5
18/18 [==============================] - 0s 28ms/step - loss: 0.3239 - val_loss: 0.5148
Epoch 81/100
16/18 [=========================>....] - ETA: 0s - loss: 0.3265
Epoch 00081: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.81-0.56.hdf5
18/18 [==============================] - 0s 22ms/step - loss: 0.3052 - val_loss: 0.5618
Epoch 82/100
18/18 [==============================] - ETA: 0s - loss: 0.4448
Epoch 00082: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.82-0.37.hdf5
18/18 [==============================] - 0s 23ms/step - loss: 0.4448 - val_loss: 0.3696
Epoch 83/100
17/18 [===========================>..] - ETA: 0s - loss: 0.3657
Epoch 00083: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.83-0.62.hdf5
18/18 [==============================] - 1s 30ms/step - loss: 0.3505 - val_loss: 0.6183
Epoch 84/100
16/18 [=========================>....] - ETA: 0s - loss: 0.3482
Epoch 00084: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.84-0.52.hdf5
18/18 [==============================] - 0s 21ms/step - loss: 0.3264 - val_loss: 0.5154
Epoch 85/100
17/18 [===========================>..] - ETA: 0s - loss: 0.2805
Epoch 00085: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.85-0.30.hdf5
18/18 [==============================] - 1s 38ms/step - loss: 0.3096 - val_loss: 0.2981
Epoch 86/100
16/18 [=========================>....] - ETA: 0s - loss: 0.3021
Epoch 00086: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.86-0.39.hdf5
18/18 [==============================] - 0s 24ms/step - loss: 0.2866 - val_loss: 0.3933
Epoch 87/100
17/18 [===========================>..] - ETA: 0s - loss: 0.2970
Epoch 00087: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.87-0.33.hdf5
18/18 [==============================] - 0s 22ms/step - loss: 0.2933 - val_loss: 0.3307
Epoch 88/100
18/18 [==============================] - ETA: 0s - loss: 0.3658
Epoch 00088: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.88-0.52.hdf5
18/18 [==============================] - 1s 29ms/step - loss: 0.3658 - val_loss: 0.5203
Epoch 89/100
18/18 [==============================] - ETA: 0s - loss: 0.3161
Epoch 00089: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.89-0.32.hdf5
18/18 [==============================] - 0s 21ms/step - loss: 0.3161 - val_loss: 0.3192
Epoch 90/100
13/18 [====================>.........] - ETA: 0s - loss: 0.2754
Epoch 00090: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.90-0.57.hdf5
18/18 [==============================] - 0s 26ms/step - loss: 0.2658 - val_loss: 0.5669
Epoch 91/100
17/18 [===========================>..] - ETA: 0s - loss: 0.2716
Epoch 00091: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.91-0.17.hdf5
18/18 [==============================] - 1s 36ms/step - loss: 0.2744 - val_loss: 0.1738
Epoch 92/100
18/18 [==============================] - ETA: 0s - loss: 0.2663
Epoch 00092: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.92-0.33.hdf5
18/18 [==============================] - 1s 35ms/step - loss: 0.2663 - val_loss: 0.3342
Epoch 93/100
17/18 [===========================>..] - ETA: 0s - loss: 0.3751
Epoch 00093: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.93-0.46.hdf5
18/18 [==============================] - 0s 20ms/step - loss: 0.3691 - val_loss: 0.4599
Epoch 94/100
17/18 [===========================>..] - ETA: 0s - loss: 0.3621
Epoch 00094: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.94-0.59.hdf5
18/18 [==============================] - 1s 29ms/step - loss: 0.3687 - val_loss: 0.5943
Epoch 95/100
17/18 [===========================>..] - ETA: 0s - loss: 0.3084
Epoch 00095: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.95-0.25.hdf5
18/18 [==============================] - 1s 37ms/step - loss: 0.3014 - val_loss: 0.2490
Epoch 96/100
18/18 [==============================] - ETA: 0s - loss: 0.3031
Epoch 00096: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.96-0.44.hdf5
18/18 [==============================] - 0s 22ms/step - loss: 0.3031 - val_loss: 0.4376
Epoch 97/100
16/18 [=========================>....] - ETA: 0s - loss: 0.3534
Epoch 00097: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.97-0.54.hdf5
18/18 [==============================] - 1s 30ms/step - loss: 0.3925 - val_loss: 0.5428
Epoch 98/100
18/18 [==============================] - ETA: 0s - loss: 0.2995
Epoch 00098: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.98-0.21.hdf5
18/18 [==============================] - 0s 25ms/step - loss: 0.2995 - val_loss: 0.2076
Epoch 99/100
18/18 [==============================] - ETA: 0s - loss: 0.2628
Epoch 00099: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.99-0.29.hdf5
18/18 [==============================] - 0s 26ms/step - loss: 0.2628 - val_loss: 0.2883
Epoch 100/100
16/18 [=========================>....] - ETA: 0s - loss: 0.2956
Epoch 00100: saving model to /content/drive/My Drive/study_ai_ml_google/skl_ml/out/checkpoints/weights.100-0.28.hdf5
18/18 [==============================] - 1s 33ms/step - loss: 0.2835 - val_loss: 0.2774
In [18]:
y_pred = estimator.predict(x_train)
18/18 [==============================] - 1s 6ms/step
In [19]:
plt.title('NonLiner Regressions via DL by ReLU')
plt.plot(data, target, 'o')
plt.plot(data, true_func(data), '.')
plt.plot(x_train, y_pred, "o", label='predicted: deep learning')
#plt.legend(loc=2)
  #【コメント】真の関数にだいぶ近いがばらつき大きいところはずれている0.4,1.0あたり
  #【コメント】0.2あたりで下に引っ張られそうだけど真の関数とほぼあってるのはなぜ?
Out[19]:
[<matplotlib.lines.Line2D at 0x7f7f900931d0>]
In [20]:
print(lasso_clf.coef_)
[-0. -0. -0. -0. -0. -0. -0. -0. -0. -0. -0. -0. -0. -0. -0. -0. -0. -0.
 -0. -0. -0. -0. -0. -0. -0. -0. -0. -0. -0. -0. -0. -0. -0. -0. -0. -0.
 -0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
In [20]:
 
In [ ]:
from google.colab import files
import re

uploaded = files.upload()

for fn in uploaded.keys():
  fn_s = re.escape(fn)
  output_fn = fn.split('.', 1)[0]+'.html'
  output_fn_s = re.escape(output_fn)
  !jupyter nbconvert --to html $fn_s
  files.download(output_fn)
  !rm $fn_s
Upload widget is only available when the cell has been executed in the current browser session. Please rerun this cell to enable.