首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Tensorflow在评估我的测试模型时遇到问题

Tensorflow在评估我的测试模型时遇到问题
EN

Stack Overflow用户
提问于 2019-02-26 23:10:52
回答 1查看 25关注 0票数 0

下面是我的简单代码。目标是从数据中预测y=x²曲线。

代码语言:javascript
复制
import matplotlib.pyplot as plt
import tensorflow as tf
%matplotlib inline

#input data
x_data = np.linspace(0,10,11)
y_data=x_data**2

#Data for testing
x_test = np.linspace(0,10,11)

#placeholders
X = tf.placeholder("float32", [None, 11])
Y = tf.placeholder("float32", [None, 11])

#Neural Network Model
def multilayer_perceptron(X, weights, biases):
    layer_1 = tf.add(tf.matmul(X, weights['h1']), biases['b1'])
    layer_1 = tf.nn.sigmoid(layer_1)
    out_layer = tf.matmul(layer_1, weights['out']) + biases['out']
    return out_layer

# Store layers weight & bias
weights = {
    'h1': tf.Variable(tf.random_normal([11, 2], 0, 0.1)),
    'out': tf.Variable(tf.random_normal([2, 1], 0, 0.1))
}
biases = {
    'b1': tf.Variable(tf.random_normal([2], 0, 0.1)),
    'out': tf.Variable(tf.random_normal([1], 0, 0.1))
}

# Construct model
pred = multilayer_perceptron(X, weights, biases)

# Define loss and optimizer
cost = tf.reduce_mean(tf.square(pred-Y))
optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost)

init = tf.global_variables_initializer()

#Session Eval
with tf.Session() as sess:
    sess.run(init)
    epochs = 10000
    for i in range(epochs):
        o, c, p = sess.run([optimizer, cost, pred], feed_dict={X: x_data.reshape([1,11]),Y: y_data.reshape([1,11])})
    pred_value = sess.run([pred],feed_dict={X: x_test.reshape([1,11])})

最后一行是使用x_test作为输入调用一个会话来计算pred,对吗?当我运行这段代码时,pred_value只是一个随机数,而不是11个可能的正确值。我不知道代码出了什么问题

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-27 02:13:02

这是因为输出层的形状是(None, 1)。它应该是(None, 11)才能得到你想要的输出。因此,您必须将weights['out']的形状更改为(2,11),将biases['out']的形状更改为(11)

完整的工作代码如下。

代码语言:javascript
复制
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
# %matplotlib inline

#input data
x_data = np.linspace(0,10,11)
y_data=x_data**2

#Data for testing
x_test = np.linspace(0,10,11)

#placeholders
X = tf.placeholder("float32", [None, 11])
Y = tf.placeholder("float32", [None, 11])

#Neural Network Model
def multilayer_perceptron(X, weights, biases):
    layer_1 = tf.add(tf.matmul(X, weights['h1']), biases['b1'])
    layer_1 = tf.nn.sigmoid(layer_1)
    out_layer = tf.matmul(layer_1, weights['out']) + biases['out']
    print (out_layer.shape)
    return out_layer

# Store layers weight & bias
weights = {
    'h1': tf.Variable(tf.random_normal([11, 2], 0, 0.1)),
    'out': tf.Variable(tf.random_normal([2, 11], 0, 0.1))
}
biases = {
    'b1': tf.Variable(tf.random_normal([2], 0, 0.1)),
    'out': tf.Variable(tf.random_normal([11], 0, 0.1))
}

# Construct model
pred = multilayer_perceptron(X, weights, biases)

# Define loss and optimizer
cost = tf.reduce_mean(tf.square(pred-Y))
optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost)

init = tf.global_variables_initializer()

#Session Eval
with tf.Session() as sess:
    sess.run(init)
    epochs = 10000
    for i in range(epochs):
        o, c, p = sess.run([optimizer, cost, pred], feed_dict={X: x_data.reshape([1,11]),Y: y_data.reshape([1,11])})
        # print (p.shape)
    pred_value = sess.run([pred],feed_dict={X: x_test.reshape([1,11])})
    print (pred_value)

输出如下所示。

代码语言:javascript
复制
[array([[ 0.       ,  1.       ,  3.9999986,  8.999985 , 15.999548 ,
        23.496645 , 26.745462 , 28.187344 , 28.973213 , 28.841356 ,
        29.02704  ]], dtype=float32)]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54888586

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档