重塑的 Tensorflow 维度问题
Tensorflow dimensionality issue with reshape
我已经创建了这段代码,但我遇到了维度错误
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
from tensorflow.contrib.rnn.python.ops import rnn_cell, rnn
from time import time
# 2) Import MNIST data http://yann.lecun.com/exdb/mnist/
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
x_train = mnist.train.images
# Define the appropriate model and variables (USER INPUTS)
batch = 100 # Define the size of the batch
units = 32 # Number of units of each network
recurrent_layers = 1 # Number of layers
nnclasses = 10 # MNIST classes (0-9)
steps = x_train.shape[1] # 784
feed = 1 # Number of pixels to be fed into the model
recurrent_layers = 1 # Define the size of the recurrent layers
dropout = 1 #
x = tf.placeholder(tf.float32,[None, None]) # batch(100)x784
x_resh = tf.reshape(x,[-1,steps,1]) # (100, 784, 1)
keep_prob = tf.placeholder(tf.float32,shape=[])
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
w_fc = weight_variable([units, nnclasses])
cell = tf.contrib.rnn.GRUCell(units)
cell = tf.contrib.rnn.DropoutWrapper(cell, input_keep_prob = keep_prob)
cell = tf.contrib.rnn.MultiRNNCell([cell] * recurrent_layers)
cell = tf.contrib.rnn.DropoutWrapper(cell, output_keep_prob = keep_prob)
outputs, final_state = tf.nn.dynamic_rnn(cell, x_resh, dtype=tf.float32)
output = outputs[:,:-1, :]
logits = tf.matmul(tf.reshape(output,[-1,tf.shape(w_fc)[0]]), w_fc) # [78300, 10]
y = tf.reshape(x[:,1:], [-1, nnclasses]) # [7830, 10]
K = [tf.shape(y)[0], tf.shape(logits)[0]]
sess = tf.InteractiveSession()
sess.run(tf.global_variables_initializer())
def binarize(images, threshold=0.1):
return (threshold < images).astype('float32')
batch_x, _ = mnist.train.next_batch(batch)
batch_x = binarize(batch_x, threshold=0.1)
return = sess.run(K, feed_dict={x: batch_x, keep_prob: 1.0})
哪个 return [7830, 78300]。问题是这两个数字应该是一样的。它们是 y 和 logits 的行,如果它们不相似,我无法在交叉熵设置中比较它们。有人可以让我知道这个过程哪里错了吗?实际上, (y) 应该 return [78300, 10] 但我不知道为什么。
y = tf.reshape(x[:,1:], [-1, nnclasses]) # [7830, 10]
您的 x
张量的形状为 batch(100)x784
,因此 x[:1,:]
为 100x783。总共有 78,300 个元素。 78300x10 将是 783,000,您只是在 x 中没有足够的数据来使它成为那个形状。
您的意思是使用 logits 作为 y 的参数吗?假设 y 是你的输出,使用 x 作为参数意味着你已经绕过了整个网络。
我已经创建了这段代码,但我遇到了维度错误
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
from tensorflow.contrib.rnn.python.ops import rnn_cell, rnn
from time import time
# 2) Import MNIST data http://yann.lecun.com/exdb/mnist/
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
x_train = mnist.train.images
# Define the appropriate model and variables (USER INPUTS)
batch = 100 # Define the size of the batch
units = 32 # Number of units of each network
recurrent_layers = 1 # Number of layers
nnclasses = 10 # MNIST classes (0-9)
steps = x_train.shape[1] # 784
feed = 1 # Number of pixels to be fed into the model
recurrent_layers = 1 # Define the size of the recurrent layers
dropout = 1 #
x = tf.placeholder(tf.float32,[None, None]) # batch(100)x784
x_resh = tf.reshape(x,[-1,steps,1]) # (100, 784, 1)
keep_prob = tf.placeholder(tf.float32,shape=[])
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
w_fc = weight_variable([units, nnclasses])
cell = tf.contrib.rnn.GRUCell(units)
cell = tf.contrib.rnn.DropoutWrapper(cell, input_keep_prob = keep_prob)
cell = tf.contrib.rnn.MultiRNNCell([cell] * recurrent_layers)
cell = tf.contrib.rnn.DropoutWrapper(cell, output_keep_prob = keep_prob)
outputs, final_state = tf.nn.dynamic_rnn(cell, x_resh, dtype=tf.float32)
output = outputs[:,:-1, :]
logits = tf.matmul(tf.reshape(output,[-1,tf.shape(w_fc)[0]]), w_fc) # [78300, 10]
y = tf.reshape(x[:,1:], [-1, nnclasses]) # [7830, 10]
K = [tf.shape(y)[0], tf.shape(logits)[0]]
sess = tf.InteractiveSession()
sess.run(tf.global_variables_initializer())
def binarize(images, threshold=0.1):
return (threshold < images).astype('float32')
batch_x, _ = mnist.train.next_batch(batch)
batch_x = binarize(batch_x, threshold=0.1)
return = sess.run(K, feed_dict={x: batch_x, keep_prob: 1.0})
哪个 return [7830, 78300]。问题是这两个数字应该是一样的。它们是 y 和 logits 的行,如果它们不相似,我无法在交叉熵设置中比较它们。有人可以让我知道这个过程哪里错了吗?实际上, (y) 应该 return [78300, 10] 但我不知道为什么。
y = tf.reshape(x[:,1:], [-1, nnclasses]) # [7830, 10]
您的 x
张量的形状为 batch(100)x784
,因此 x[:1,:]
为 100x783。总共有 78,300 个元素。 78300x10 将是 783,000,您只是在 x 中没有足够的数据来使它成为那个形状。
您的意思是使用 logits 作为 y 的参数吗?假设 y 是你的输出,使用 x 作为参数意味着你已经绕过了整个网络。