两个张量相乘 [英] Multiplying two tensors
本文介绍了两个张量相乘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 Tensorflow 中有两个张量,它们有以下两种形状:
I have the two tensors in Tensorflow, which have the following two shapes:
print(tf.valid_dataset.get_shape())
print(weights1.get_shape())
有结果:
(10000, 784)
(784, 1024)
但是,如果我尝试将它们相乘,就像这样:
However, if I try to multiply them, like this:
tf.matmul(tf_valid_dataset, weights1)
我明白了:
Tensor("Variable:0", shape=(784, 1024), dtype=float32_ref) must be from the same graph as Tensor("Const:0", shape=(10000, 784), dtype=float32).
因为我是在它们都具有 784
大小的一侧乘以它们,所以这对我来说似乎是正确的.
Since I am multiplying them on the side where they both have the size 784
, this seems correct to me.
知道哪里出了问题吗?
我在打印语句之前的代码是这样的:
The code I have before the print statements is this:
num_hidden_nodes=1024
batch_size = 128
learning_rate = 0.5
graph = tf.Graph()
with graph.as_default():
tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size*image_size))
tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels))
tf.valid_dataset = tf.constant(valid_dataset)
tf.test_dataset = tf.constant(test_dataset)
weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_hidden_nodes]))
biases1 = tf.Variable(tf.zeros([num_hidden_nodes]))
weights2 = tf.Variable(tf.truncated_normal([num_hidden_nodes, num_labels]))
biases2 = tf.Variable(tf.zeros([num_labels]))
weights = [weights1, biases1, weights2, biases2]
lay1_train = tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1)
logits = tf.matmul(lay1_train, weights2) + biases2
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=tf_train_labels))
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)
推荐答案
您的代码似乎正确.请再次检查.通过运行以下代码来验证它:
Your code seems correct. Please check again. Verify it by running the below code:
num_hidden_nodes=1024
batch_size = 1000
learning_rate = 0.5
image_size = 28
num_labels = 10
tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size*image_size))
tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels))
# tf.valid_dataset = tf.constant(valid_dataset)
# tf.test_dataset = tf.constant(test_dataset)
weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_hidden_nodes]))
biases1 = tf.Variable(tf.zeros([num_hidden_nodes]))
weights2 = tf.Variable(tf.truncated_normal([num_hidden_nodes, num_labels]))
biases2 = tf.Variable(tf.zeros([num_labels]))
weights = [weights1, biases1, weights2, biases2]
lay1_train = tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1)
logits = tf.matmul(lay1_train, weights2) + biases2
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=tf_train_labels))
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)
sess.run(tf.initialize_all_variables())
input_data = np.random.randn(batch_size, 784)
input_labels = [np.random.randint(0,10) for _ in xrange(batch_size)]
import sklearn.preprocessing
label_binarizer = sklearn.preprocessing.LabelBinarizer()
transformed_labels = label_binarizer.fit_transform(input_labels)
sess.run(optimizer,feed_dict={tf_train_dataset:input_data, tf_train_labels:transformed_labels})
这篇关于两个张量相乘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文