【问题标题】:Backpropagation Using Tensorflow and Numpy MSE not Dropping使用 Tensorflow 和 Numpy MSE 的反向传播不会丢弃
【发布时间】:2018-07-22 08:52:47
【问题描述】:

我正在尝试创建反向传播,但我不想使用 TF 中的 GradientDescentOptimizer。我只是想更新我自己的权重和偏见。问题是均方误差或成本没有接近于零。它只是保持在 0.2xxx 左右。是因为我的输入是 520x1600(是的,每个输入有 1600 个单元,是的,有 520 个)还是我隐藏层中的神经元数量有问题?我已经尝试使用 GradientDescentOptimizer 和 minimize(cost) 来实现这一点,它运行良好(随着训练的进行,成本降低到接近于零)但我的代码中可能存在更新权重和偏差的问题。

这是我的代码:

import tensorflow as tf
import numpy as np
from BPInputs40 import pattern, desired;

#get the inputs and desired outputs, 520 inputs, each has 1600 units
train_in = pattern
train_out = desired

learning_rate=tf.constant(0.5)
num_input_neurons = len(train_in[0])
num_output_neurons = len(train_out[0])
num_hidden_neurons = 20

#weight matrix initialization with random values
w_h = tf.Variable(tf.random_normal([num_input_neurons, num_hidden_neurons]), dtype=tf.float32)
w_o = tf.Variable(tf.random_normal([num_hidden_neurons, num_output_neurons]), dtype=tf.float32)
b_h = tf.Variable(tf.random_normal([1, num_hidden_neurons]), dtype=tf.float32)
b_o = tf.Variable(tf.random_normal([1, num_output_neurons]), dtype=tf.float32)

# Model input and output
x = tf.placeholder("float")
y = tf.placeholder("float")

def sigmoid(v):
  return tf.div(tf.constant(1.0),tf.add(tf.constant(1.0),tf.exp(tf.negative(v*0.001))))
def derivative(v):
  return tf.multiply(sigmoid(v), tf.subtract(tf.constant(1.0), sigmoid(v)))

output_h = tf.sigmoid(tf.add(tf.matmul(x,w_h),b_h))
output_o = tf.sigmoid(tf.add(tf.matmul(output_h,w_o),b_o)) 

error = tf.subtract(output_o,y)  #(1x35)
mse = tf.reduce_mean(tf.square(error))

delta_o=tf.multiply(error,derivative(output_o)) 
delta_b_o=delta_o
delta_w_o=tf.matmul(tf.transpose(output_h), delta_o)
delta_backprop=tf.matmul(delta_o,tf.transpose(w_o))
delta_h=tf.multiply(delta_backprop,derivative(output_h))
delta_b_h=delta_h
delta_w_h=tf.matmul(tf.transpose(x),delta_h)

#updating the weights
train = [
  tf.assign(w_h, tf.subtract(w_h, tf.multiply(learning_rate, delta_w_h))),
  tf.assign(b_h, tf.subtract(b_h, tf.multiply(learning_rate, tf.reduce_mean(delta_b_h, 0)))), 
  tf.assign(w_o, tf.subtract(w_o, tf.multiply(learning_rate, delta_w_o))), 
  tf.assign(b_o, tf.subtract(b_o, tf.multiply(learning_rate, tf.reduce_mean(delta_b_o, 0))))
]

sess = tf.Session()
sess.run(tf.global_variables_initializer())

err,target=1, 0.005
epoch, max_epochs = 0, 2000000
while epoch < max_epochs:
  epoch += 1
  err, _ = sess.run([mse, train],{x:train_in,y:train_out})
  if (epoch%1000 == 0):
    print('Epoch:', epoch, '\nMSE:', err)

answer = tf.equal(tf.floor(output_o + 0.5), y)
accuracy = tf.reduce_mean(tf.cast(answer, "float"))
print(sess.run([output_o], feed_dict={x: train_in, y: train_out}));
print("Accuracy: ", (1-err) * 100 , "%");

更新:我现在开始工作了。一旦我增加隐藏层中的神经元数量,MSE 几乎下降到零。我尝试将 5200 和 6400 个神经元用于隐藏层,并且仅使用 5000 个 epoch,准确率几乎达到 99%。另外,我使用的最大学习率是 0.1,因为当高于该值时,MSE 不会接近于零。

【问题讨论】:

    标签: numpy tensorflow backpropagation


    【解决方案1】:

    我不是该领域的专家,但您的权重似乎已正确更新。您的 MSE 从一些较高的值降低到 0.2xxx 的事实就是一个强有力的指标。我肯定会尝试用更多隐藏的神经元(例如 500 个)来解决这个问题

    顺便说一句,您的输入是否标准化?如果不是,那显然可能是原因

    【讨论】:

    • 我的输入只是 0 和 1。就像黑色像素一样,白色像素为 1 和 0。我每个样本的图像大小是 40x40,这就是为什么我有 1600 个输入神经元。
    猜你喜欢
    • 1970-01-01
    • 2021-07-14
    • 2018-08-30
    • 1970-01-01
    • 1970-01-01
    • 2020-09-09
    • 1970-01-01
    • 2019-04-13
    • 2021-07-18
    相关资源
    最近更新 更多