首先,上代码:
from keras.models import Sequential
from keras.optimizers import SGD
from keras.layers import Dense,Activation
from keras.constraints import unit_norm
import numpy as np
import matplotlib.pyplot as plt
in_x = [1,2,3,4]
in_y = [2,5,6,9]
model = Sequential()
model.add(Dense(units=1,kernel_initializer="ones",kernel_constraint=unit_norm(),input_dim=1))
model.compile(optimizer='adam',loss='mse')
history = model.fit(in_x,in_y,epochs=10000,verbose=1)
w,b = model.layers[0].get_weights()
print('w:',w,'b:',b)
import matplotlib.pyplot as plt
y_pred = model.predict(in_x)
plt.scatter(in_x,in_y)
plt.plot(in_x,y_pred,'r-',lw=3)
plt.show()
100epochs
1000epochs
5000epochs
可见图形方向是确定的,通过训练,移动了图像。
网友评论