2015-12-04 124 views
2

我试着写在张量流(逻辑运算),有两个输入和两个权重乘以他们得到一个号码,这个号码添加到偏见,我的问题MATMUL一将X(输入)和W(重量)发送到方法中。 (垂直) [[1], [1]] 以及[W](水平)的[0.49900547,0.49900547]得到一个数字作为结果,但它给了我两个数字,我怎么能乘上正确的? ? 这是我的代码>>tf.matmul不按预期工作

import tensorflow as tf 
import numpy 
rng = numpy.random 

# Parameters 
learning_rate = 0.01 
training_epochs = 2000 
display_step = 50 

# Training Data 
train_X = numpy.asarray([[[1.0],[1.0]],[[1.0],[0.0]],[[0.0],[1.0]],[[0.0],[0.0]]]) 
train_Y = numpy.asarray([1.0,0.0,0.0,0.0]) 
n_samples = train_X.shape[0] 

# tf Graph Input 
X = tf.placeholder("float",[2,1],name="inputarr") 
Y = tf.placeholder("float",name = "outputarr") 

# Create Model 

# Set model weights 
W = tf.Variable(tf.zeros([1,2]), name="weight") 
b = tf.Variable(rng.randn(), name="bias") 

# Construct a linear model 
activation = tf.add(tf.matmul(X,W), b) 
mulres = tf.matmul(X,W) 

# Minimize the squared errors 
cost = tf.reduce_sum(tf.pow(activation-Y, 2))/(2*n_samples) #L2 loss 
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost) #Gradient descent 

# Initializing the variables 
init = tf.initialize_all_variables() 

# Launch the graph 
with tf.Session() as sess: 
    sess.run(init) 

    # Fit all training data 
    for epoch in range(training_epochs): 
     for (x, y) in zip(train_X, train_Y): 
      sess.run(optimizer, feed_dict={X: x, Y: y}) 

     #Display logs per epoch step 
     if epoch % display_step == 0: 
      print "Epoch:", '%04d' % (epoch+1), \ 
       "W=", sess.run(W), "b=", sess.run(b) , "x= ",x," y =", y," result :",sess.run(mulres,feed_dict={X: x}) 

    print "Optimization Finished!" 
    print "W=", sess.run(W), "b=", sess.run(b), '\n' 


    # Testing example, as requested (Issue #2) 
    test_X = numpy.asarray([[1.0,0.0]]) 
    test_Y = numpy.asarray([0]) 

    for x, y in zip(train_X, train_Y): 
     print "x: ",x,"y: ",y 
     print "Testing... (L2 loss Comparison)","result :",sess.run(mulres, feed_dict={X: x}) 
     print sess.run(tf.matmul(X, W),feed_dict={X: x}) 
     print "result :" 
     predict = sess.run(activation,feed_dict={X: x}) 
     print predict 

回答

3

与标准矩阵乘法,如果A具有形状[m, k]B具有形状[k, n],然后tf.matmul(A, B)具有(在TensorFlow使用顺序m行,列n)形状[m, n]

在你的程序中,你是计算tf.matmul(X, W)X被定义为形状为[2, 1]的占位符; W被定义为变量初始化为0的矩阵。因此,mulres = tf.matmul(X, W)将形成[2, 2],这是我在本地运行代码时打印的内容(result: ...)。

如果你想用一个单独的输出定义一个隐藏层,这种变化很简单:

W = tf.Variable(tf.zeros([1,2]), name="weight") 

...应改为:

W = tf.Variable(tf.zeros([2, 1]), name="weight") 

(事实上,初始化你的权重以tf.zeros会阻止它的训练,因为所有的输入元素将得到相同的梯度反传相反,你应该使用随机初始化它们,例如:

W = tf.Variable(tf.truncated_normal([2, 1], stddev=0.5), name="weight") 

这将使网络能够了解重量的每个组成部分的不同值。)

2

matmul上你的情况有2行1列张量直接操作。

有一个在matmul参数进行转任项,如:

matmul(X, W, transpose_a=True) 

您可以检查出这里的文档:docs