简体   繁体   English

在 Keras 中显示 'y' 的预测值和相应的实际 'x' 值

[英]Displaying predicted values of 'y' with corresponding actual 'x' values in Keras

I am attempting to write a neural network that uses 100 data points evenly spaced along ay = sin(x) graph, between 0 and (5/2)pi.我正在尝试编写一个神经网络,它使用 100 个数据点沿 y = sin(x) 图均匀分布,介于 0 和 (5/2)pi 之间。 So the x values go from 0 to 7.854, and the corresponding y values oscillate between 1 and -1.因此 x 值从 0 到 7.854,相应的 y 值在 1 和 -1 之间波动。

I have split the data into training and validation points, and it seems to have trained properly, however when I try to print the 'test' data, it is 100% correct, no matter how many neurons, epochs that I use.我已将数据分成训练点和验证点,并且似乎训练得当,但是当我尝试打印“测试”数据时,无论我使用多少个神经元和 epoch,它都是 100% 正确的。 I think it is simply displaying the actual values, no predictions.我认为它只是显示实际值,没有预测。

I think that I must have done something wrong in the final predictions part of the code, but I am not sure how to display the predicted values of y by the network for corresponding x values.我想我一定是在代码的最终预测部分做错了什么,但我不确定如何通过网络显示相应 x 值的 y 预测值。 Any help would be greatly appreciated!任何帮助将不胜感激!

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Activation
import pandas as pd
import io
import os
import requests
import numpy as np
from sklearn import metrics
from sklearn.model_selection import train_test_split
from tensorflow.keras.callbacks import EarlyStopping

df = pd.read_csv("C:\\Users\\Dan\\Desktop\\UNI\\PROJECT\\y_sinx_values.csv")

x = df['x'].values 
y = df['y'].values

# Split into training and validation sets
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25, random_state=42)

model = Sequential()
model.add(Dense(100, input_shape = (1,), activation='relu'))
model.add(Dense(50, activation='relu'))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer = 'adam')

monitor = EarlyStopping(monitor='val_loss', min_delta=1e-3, patience=1, verbose=1, mode='auto', restore_best_weights=True)
model.fit(x_train,y_train,validation_data=(x_test,y_test),callbacks=[monitor],verbose=2, epochs=2)

pred = model.predict(x_test)
score = np.sqrt(metrics.mean_squared_error(pred,y_test))
print(f"Final score (RMSE): {score}")

# Sample predictions
for i in range(25):
    print(f"{i+1}. x: {x_test[i]}, y: {y_test[i]}")
# Sample predictions
for i in range(25):
    print(f"{i+1}. x: {x_test[i]}, y: {y_test[i]}")

Yes, you are printing your input test data.是的,您正在打印输入测试数据。 You most likely want something like:你很可能想要这样的东西:

# Sample predictions
for i in range(25):
    print(f"{i+1}. x: {x_test[i]}, y: {pred[i]}")

Note the y_test[i] has been changed to pred[i] .请注意y_test[i]已更改为pred[i]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM