我最近开始研究 ANN,并决定制作一个简单的 ANN,它能够预测某个弧度的正弦值。准备好的数据:
args_len = 200
input = np.random.uniform(0,2*np.pi, args_len).reshape(-1,1)
output = np.sin(input).reshape(-1,1)#np.random.uniform(-1,1,args_len).reshape(-1,1)
data = np.hstack((input, output) )
input_train_data, input_test_data, output_train_data, output_test_data = train_test_split(data[:,0], data[:,1], test_size=0.3)
input_train_data = input_train_data.reshape(-1,1)
input_test_data = input_test_data.reshape(-1,1)
output_test_data = output_test_data.reshape(-1,1)
output_train_data = output_train_data.reshape(-1,1)
fig = px.scatter(x=data[:,0], y=data[:,1], title="y=sin(rad)")
fig.show()
我在 keras 上构建了一个人工神经网络并进行了训练:
model = keras.Sequential()
model.add( Dense(units=1, activation="sigmoid", input_shape=(1,)) )
model.add( Dense(units=1, activation="sigmoid") )
model.add( Dense(units=1, activation="sigmoid") )
model.compile(loss="mean_squared_error", optimizer="Adamax")
model.fit(input_train_data, output_train_data, epochs=1500, batch_size=20)
result = model.evaluate(input_test_data, output_test_data,verbose=2)
result = model.evaluate(input_test_data, output_test_data, verbose=1)
print(result)
>>0.34740047256151835
然后我尝试预测测试数据:
predict_input = input_test_data
predict_output = model.predict(predict_input)
fig = px.scatter(x = predict_input.reshape(1,-1)[0], y = predict_output.reshape(1,-1)[0], title="y=sin(rad)")
fig.show()
一般来说,我们需要更多的层维度和输出的线性激活: