美文网首页
Keras_regression

Keras_regression

作者: VaultHunter | 来源:发表于2018-03-16 10:27 被阅读0次
import numpy as np
np.random.seed(1337)
from keras.models import Sequential
from keras.layers import Dense
import matplotlib.pyplot as plt

X = np.linspace(-1,1,200)
np.random.shuffle(X)
Y = 0.5 * X + 2 + np.random.normal(0,0.05,(200,))

plt.scatter(X,Y)
plt.show()


X_train,Y_train = X[:160],Y[:160]
X_test,Y_test = X[160:],Y[160:]

model = Sequential()
model.add(Dense(1, input_dim=1))
model.compile(loss = 'mse',optimizer = 'sgd')

print('training..........')
for step in range(1000):
    cost = model.train_on_batch(X_train,Y_train)
    if step % 100 == 0:
        print('train cost:',cost)
        
print('testing...........')
cost = model.evaluate(X_test,Y_test,batch_size= 40)
print('test cost:',cost)
W,b = model.layers[0].get_weights()
print('W=',W,'b=',b)

Y_pred =  model.predict(X_test)
plt.scatter(X_test,Y_test)
plt.plot(X_test,Y_pred)
plt.show()

中文文档

相关文章

  • Keras_regression

    中文文档

  • keras_regression

    问题:神经网络可以用来模拟回归问题 (regression),例如给下面一组数据,用一条线来对数据进行拟合,并可以...

网友评论

      本文标题:Keras_regression

      本文链接:https://www.haomeiwen.com/subject/mijlqftx.html