gpt4 book ai didi

python - 为什么模型预测在 tensorflow (python)中没有更新?

转载 作者:行者123 更新时间:2023-11-30 09:02:52 25 4
gpt4 key购买 nike

我根据一些不同的在线资源构建了一个 tensorflow 图,并尝试将曲线拟合到y=x^2。我使用两个隐藏层,分别有 25 个和 10 个神经元。代码完成,但成本函数从未减少,并且 x_testpreds 的最终图表显然是错误的。我查阅了很多不同的资源和一些 O'reilly 的书籍,但我终其一生都无法弄清楚我哪里出了问题。任何帮助将不胜感激。

import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
import tensorflow as tf
from sklearn.model_selection import train_test_split

tf.reset_default_graph()
# Create Data
X_range = np.arange(-50,50,0.1)
Y_range = (X_range ** 2)

data = pd.DataFrame({"x": X_range, "y":Y_range})
x = data['x'].values.reshape(1000,1)
y = data['y'].values.reshape(1000,)
x_train, x_test, y_train, y_test = train_test_split(x,y, test_size = .30, random_state = 42)

c_t = []

# Create NNModel function
def neural_net_model(x_data, input_dim):
# Build the First Layer
W_1 = tf.Variable(tf.random_uniform([input_dim, 25]))
b_1 = tf.Variable(tf.zeros([25]))
layer_1 = tf.add(tf.matmul(x_data, W_1), b_1)
layer_1 = tf.nn.relu(layer_1)

# Build the Second Layer
W_2 = tf.Variable(tf.random_uniform([25, 10]))
b_2 = tf.Variable(tf.zeros([10]))
layer_2 = tf.add(tf.matmul(layer_1, W_2), b_2)
layer_2 = tf.nn.relu(layer_2)

# Build output
W_output = tf.Variable(tf.random_uniform([10, 1]))
b_output = tf.Variable(tf.zeros([1]))
output = tf.add(tf.matmul(layer_2, W_output), b_output)

return output

xs = tf.placeholder(dtype=tf.float32, name="xs")
ys = tf.placeholder(dtype=tf.float32, name='ys')

output = neural_net_model(xs, 1)
cost = tf.reduce_mean(tf.square(output-ys))
train = tf.train.GradientDescentOptimizer(.005).minimize(cost)

with tf.Session() as sess:
sess.run(tf.global_variables_initializer())

# Run for 100 Epochs
for i in range(100):
for j in range(x_train.shape[0]):
sess.run(train, feed_dict={xs: x_train[j].reshape(1,1), ys: y_train[j]})

if i % 10 == 0:
c_t.append(sess.run(cost, feed_dict={xs:x_train, ys:y_train}))
print('Epoch :',i,'Cost :',c_t[-1])

pred = sess.run(output, feed_dict={xs: x_test})

print("cost: ", sess.run(cost, feed_dict={xs: x_train, ys: y_train}))

plt.scatter(x_test,y_test,label="Original Data")
plt.scatter(x_test,pred,label="Predicted Data")
plt.legend(loc='best')
plt.ylabel('value')
plt.xlabel('x_data')
plt.title('model_fit')
plt.show()```

最佳答案

我正在玩模型,然后我想起了这个问题问题是 Y_range 在某些方面太大了,因此对于 tensorflow 来说,为了运行一些计算,内存将超出并且该值将是 inf为此,您需要将数据标准化为 mean 等于 0,std 等于 1

添加

# Create Data 
X_range = np.arange(-50,50,0.1)
Y_range = (X_range ** 2)

def normalizor(x):
mean=x.mean()
std=x.std()
return (x-mean)/std
X_range=normalizor(X_range)
Y_range=normalizor(Y_range)

加上将优化器从 GradientDiscent 更改为 Adam

train = tf.train.AdamOptimizer(.005).minimize(cost)

关于python - 为什么模型预测在 tensorflow (python)中没有更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59618870/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com