2层神经网络在TensorFlow中的实现

我刚开始学习TensorFlow,正在尝试训练以下两层网络。看起来它不起作用,因为交叉熵并未随着迭代次数的增加而减少。我认为我搞错了隐藏层与输出层的连接。如果你能看出问题所在,请帮助我,

import tensorflow as tffrom scipy.io import loadmatimport numpy as npimport sysx = loadmat('../mnist_data/ex4data1.mat')X = x['X']# one hot conversiony_temp = x['y']y_temp = np.reshape(y_temp, (len(y_temp),))y = np.zeros((len(y_temp),10))y[np.arange(len(y_temp)), y_temp-1] = 1.input_size = 400hidden1_size = 25output_size = 10num_iters = 50reg_alpha = 0.05x = tf.placeholder(tf.float32, [None, input_size], name='data')W1 = tf.Variable(tf.zeros([hidden1_size, input_size], tf.float32, name='weights_1st_layer'))b1 = tf.Variable(tf.zeros([hidden1_size], tf.float32), name='bias_layer_1')W2 = tf.Variable(tf.zeros([output_size, hidden1_size], tf.float32, name='weights_2nd_layer'))b2 = tf.Variable(tf.zeros([output_size], tf.float32), name='bias_layer_2')hidden_op = tf.nn.relu(tf.add(tf.matmul(x, W1, transpose_b=True), b1))output_op = tf.matmul(hidden_op, W2, transpose_b=True) + b2pred = tf.nn.softmax(output_op) y_ = tf.placeholder(tf.float32, [None, 10], name='actual_labels')cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(    labels=y_, logits=output_op))train_step = tf.train.GradientDescentOptimizer(reg_alpha).minimize(cross_entropy)sess = tf.InteractiveSession()tf.global_variables_initializer().run()for _ in range(50):    print ('training..', _)    print (sess.run([train_step, cross_entropy], feed_dict={x : X, y_ : y}))corr_pred = tf.equal(tf.argmax(pred, axis=1), tf.argmax(y_, axis=1))acc = tf.reduce_mean(tf.cast(corr_pred, tf.float32))print (sess.run(acc, feed_dict={x:X, y_:y}))sess.close()

回答:

尝试将你的权重初始化为随机值,而不是零。

所以不要使用:

W1 = tf.Variable(tf.zeros([hidden1_size, input_size], tf.float32, name='weights_1st_layer'))W2 = tf.Variable(tf.zeros([output_size, hidden1_size], tf.float32, name='weights_2nd_layer'))

而是使用:

W1 = tf.Variable(tf.truncated_normal([hidden1_size, input_size], tf.float32, name='weights_1st_layer'), stddev=0.1))W2 = tf.Variable(tf.truncated_normal([output_size, hidden1_size], tf.float32, name='weights_2nd_layer'), stddev=0.1))

查看这里有一个很好的总结,解释了为什么将所有权重初始化为零会阻止网络进行训练。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注