Tensorflow 基于神经网络的基本线性方程张量流估计

Tensorflow 基于神经网络的基本线性方程张量流估计,tensorflow,neural-network,Tensorflow,Neural Network,我正在努力学习Tensorflow。我正在做一个基本的例子——对方程y=x+0.1进行建模,使用神经网络进行训练,然后进行预测。我实际上采用的是乙状结肠方法(不理想),因此没有使用标准的softmax/relu方法(这对我不起作用)。代码运行,但答案是错误的:一批中的所有预测都给出了几乎相同的答案,比如y_true=[[0.356],[0.356],[0.356],[0.356],[0.356],[0.356],[0.356],[0.1,0.2,0.3,0.4]]。我做错了什么?代码如下: im

我正在努力学习Tensorflow。我正在做一个基本的例子——对方程y=x+0.1进行建模,使用神经网络进行训练,然后进行预测。我实际上采用的是乙状结肠方法(不理想),因此没有使用标准的softmax/relu方法(这对我不起作用)。代码运行,但答案是错误的:一批中的所有预测都给出了几乎相同的答案,比如y_true=[[0.356],[0.356],[0.356],[0.356],[0.356],[0.356],[0.356],[0.1,0.2,0.3,0.4]]。我做错了什么?代码如下:

import tensorflow as tf
import numpy as np

epochs = 1000
# For equation y = b + 0.1, sample data below
myImportedDatax_np = np.array([[.1],[.2],[.3],[.4]],dtype=float)
myImportedDatay_np = np.array([[.2],[.3],[.4],[.5]],dtype=float)

c = tf.constant(0.1, name='c')
b = tf.placeholder(tf.float32, [None, 1], name='b')
y = tf.add(b, c, name='y')

y_true = tf.placeholder(tf.float32, [None, 1], name='y_true')

W1 = tf.Variable(tf.random_normal([1, 3], stddev=0.03), name='W1')
b1 = tf.Variable(tf.random_normal([3]), name='b1')
W2 = tf.Variable(tf.random_normal([3, 1], stddev=0.03), name='W2')
b2 = tf.Variable(tf.random_normal([1]), name='b2')

hidden_out = tf.add(tf.matmul(b, W1), b1)
hidden_out = tf.sigmoid(hidden_out)

y_ = tf.sigmoid(tf.add(tf.matmul(hidden_out, W2), b2))

cost = tf.reduce_mean(tf.square(y_ - y_true))
optimiser = tf.train.GradientDescentOptimizer(0.005).minimize(cost)

init_op = tf.initialize_all_variables()

with tf.Session() as sess:
    # initialise the variables
    sess.run(init_op)
    for epoch in range(epochs):
        _, cost_now = sess.run([optimiser, cost], {b: myImportedDatax_np, y_true: myImportedDatay_np})
    print("Predicted values are:")
    print(sess.run(y_, {b: myImportedDatax_np}))

您的代码几乎没有什么问题:

  • 您的是一个回归问题,
    y=x+c
    ,因此请删除sigmoid输出:

    y_ = tf.add(tf.matmul(hidden_out, W2), b2)
    
  • 一个隐藏层可以更好地为您服务,您的多个隐藏单元对于这样一个简单的任务将需要更长时间的训练

  • 要处理2,将历元增加到更高的值,例如10000,并且学习率也更高,例如0.1
  • 编辑: 添加代码:

    #increased the number of epoch
    epochs = 10000
    # For equation y = b + 0.1, sample data below
    myImportedDatax_np = np.array([[.1],[.2],[.3],[.4]],dtype=float)
    myImportedDatay_np = np.array([[.2],[.3],[.4],[.5]],dtype=float)
    
    c = tf.constant(0.1, name='c')
    b = tf.placeholder(tf.float32, [None, 1], name='b')
    y = tf.add(b, c, name='y')
    
    y_true = tf.placeholder(tf.float32, [None, 1], name='y_true')
    
    W1 = tf.Variable(tf.random_normal([1, 3], stddev=0.03), name='W1')
    b1 = tf.Variable(tf.random_normal([3]), name='b1')
    W2 = tf.Variable(tf.random_normal([3, 1], stddev=0.03), name='W2')
    b2 = tf.Variable(tf.random_normal([1]), name='b2')
    
    hidden_out = tf.add(tf.matmul(b, W1), b1)
    hidden_out = tf.sigmoid(hidden_out)
    # Removed the activation
    y_ = tf.add(tf.matmul(hidden_out, W2), b2)
    
    cost = tf.reduce_mean(tf.square(y_ - y_true)
    
    #changed the learning rate
    optimiser = tf.train.GradientDescentOptimizer(0.1).minimize(cost)
    
    init_op = tf.global_variables_initializer()
    
    #Predicted values are:
    #[[ 0.19917184]
    #[ 0.30153054]
    #[ 0.40164429]
    #[ 0.4976812 ]]
    

    您的代码几乎没有什么问题:

  • 您的是一个回归问题,
    y=x+c
    ,因此请删除sigmoid输出:

    y_ = tf.add(tf.matmul(hidden_out, W2), b2)
    
  • 一个隐藏层可以更好地为您服务,您的多个隐藏单元对于这样一个简单的任务将需要更长时间的训练

  • 要处理2,将历元增加到更高的值,例如10000,并且学习率也更高,例如0.1
  • 编辑: 添加代码:

    #increased the number of epoch
    epochs = 10000
    # For equation y = b + 0.1, sample data below
    myImportedDatax_np = np.array([[.1],[.2],[.3],[.4]],dtype=float)
    myImportedDatay_np = np.array([[.2],[.3],[.4],[.5]],dtype=float)
    
    c = tf.constant(0.1, name='c')
    b = tf.placeholder(tf.float32, [None, 1], name='b')
    y = tf.add(b, c, name='y')
    
    y_true = tf.placeholder(tf.float32, [None, 1], name='y_true')
    
    W1 = tf.Variable(tf.random_normal([1, 3], stddev=0.03), name='W1')
    b1 = tf.Variable(tf.random_normal([3]), name='b1')
    W2 = tf.Variable(tf.random_normal([3, 1], stddev=0.03), name='W2')
    b2 = tf.Variable(tf.random_normal([1]), name='b2')
    
    hidden_out = tf.add(tf.matmul(b, W1), b1)
    hidden_out = tf.sigmoid(hidden_out)
    # Removed the activation
    y_ = tf.add(tf.matmul(hidden_out, W2), b2)
    
    cost = tf.reduce_mean(tf.square(y_ - y_true)
    
    #changed the learning rate
    optimiser = tf.train.GradientDescentOptimizer(0.1).minimize(cost)
    
    init_op = tf.global_variables_initializer()
    
    #Predicted values are:
    #[[ 0.19917184]
    #[ 0.30153054]
    #[ 0.40164429]
    #[ 0.4976812 ]]
    

    太好了,它起作用了。最大的区别是,正如你所说,有大量的时代,学习率也有所提高。如果我在输出中加入sigmoid,结果可能与事实相去甚远。太好了,它可以工作。最大的区别是,正如你所说,有大量的时代,学习率也有所提高。如果我在输出中加入sigmoid,结果可能与事实相差甚远。