我将一个非常简单的C ++神经网络移植到Tensorflow,然而,张量流版本并没有在训练中收敛。它是许多Netflix预测变量的组合者,并根据Netflix奖预测数据集进行操作。
<script>
function refresh_image(){
document.getElementbyId("myImage").setAttribute("src", "currentimage.php");
}
setInterval(function(){refresh_image()}, 30000);
</script>
训练运行,训练错误随着时间的推移而增长(第一次迭代与C ++版本有类似的训练错误):
from Blender import Blender, rating2internal, internal2rating
import math
import tensorflow as tf
GLOBAL_MEAN = 3.6033
NUNITS = 12
bl=Blender('/Users/dsu/src/blend/nncontrol.txt')
print('y shape:',bl.proberesults.shape )
STDDEV = tf.sqrt(1.0 /bl.nPredictors)
STDDEV2 = tf.sqrt(1.0 /NUNITS)
x = tf.placeholder(tf.float32, shape=[None, bl.nPredictors])
print('nPredictors=%d' % bl.nPredictors)
print('nRatings=%d' % bl.nRatings)
y_ = tf.placeholder(tf.float32,shape=[None, 1])
W1 = tf.Variable(tf.truncated_normal([bl.nPredictors, NUNITS], stddev=STDDEV))
b1 = tf.Variable(tf.zeros([NUNITS]))
W2 = tf.Variable(tf.truncated_normal([NUNITS,1], stddev=STDDEV))
#b2 = tf.Variable(tf.zeros([1])) #
b2 = tf.Variable(tf.fill([1], math.atanh(rating2internal(GLOBAL_MEAN))))
middle = tf.nn.tanh(tf.matmul(x, W1)+ b1)
y = tf.nn.tanh(tf.matmul(middle, W2) + b2)
loss = tf.nn.l2_loss (y - y_)
train_error = tf.sqrt( loss * 8 / bl.nRatings)
print_train_error = tf.Print(train_error, [train_error], message = 'trainning error =')
#sess = tf.Session()
sess = tf.InteractiveSession()
sess.run(tf.initialize_all_variables())
train_step = tf.train.GradientDescentOptimizer(4e-4).minimize(loss)
for i in range(20):
xs = bl.proberatings
ys = bl.proberesults
sess.run([train_step, print_train_error], feed_dict={x: xs, y_: ys})
有什么不对吗? C ++版本将训练1000次迭代,训练错误从1.0x级别一直下降到0.87级。尝试了一些变化,仍然没有收敛。