[英]tf.matmul doesn't works as expected
我嘗試在張量流中寫入和(邏輯運算),有兩個輸入和兩個權重乘以得到一個數字並將此數字加到偏差,我的問題在matmul中發送X(輸入)和W(權重)到方法在形狀。 [[1],[1]]用於X(垂直),[0.49900547,0.49900547]用於W(水平)得到一個數字作為結果,但是它給了我兩個數字,我怎么做才能乘以? 這是我的代碼>>
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
與標准矩陣乘法一樣,如果A
具有形狀[m, k]
,並且B
具有形狀[k, n]
,則tf.matmul(A, B)
具有形狀[m, n]
( m
行, n
列,訂單TensorFlow使用)。
在你的程序中,你正在計算tf.matmul(X, W)
。 X
被定義為具有形狀[2, 1]
的占位符; W
被定義為初始化為零的[1, 2]
矩陣的變量。 因此,當我在本地運行代碼時, mulres = tf.matmul(X, W)
將具有形狀[2, 2]
,這是打印的result: ...
( 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")
這將使網絡能夠為權重的每個組成部分學習不同的值。)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.