希望大家身体健康。尊敬的会员,我正在Cifar10数据集上工作,并获得TypeError:不支持/的操作数类型:"维度"和"整数"我使用相同的算法mnist数据集(灰度),但他们是没有错误的。但现在我使用相同的算法tensorflow训练Cifar10(rgb)。这个错误发生是因为(rgb)数据集?我分享我的代码,请看看谢谢
- 代码**
layer1_neuron=500
layer2_neuron=500
layer3_neuron=500
number_of_class=10
batch_size=200
#my neural network
def neural_network(x_train):
hidden_layer_1={
'weights':tf.Variable(tf.random_normal([3072,layer1_neuron])),
'biases': tf.Variable(tf.random_normal([layer1_neuron]))
}
hidden_layer_2={
'weights':tf.Variable(tf.random_normal([layer1_neuron,layer2_neuron])),
'biases':tf.Variable(tf.random_normal([layer2_neuron]))
}
hidden_layer_3={
'weights':tf.Variable(tf.random_normal([layer2_neuron,layer3_neuron])),
'biases':tf.Variable(tf.random_normal([layer3_neuron]))
}
output={
'weights':tf.Variable(tf.random_normal([layer3_neuron,number_of_class])),
'biases':tf.Variable(tf.random_normal([number_of_class]))
}
l1=tf.add(tf.matmul(x_train,hidden_layer_1['weights']),hidden_layer_1['biases'])
l1=tf.nn.relu(l1)
l2=tf.add(tf.matmul(l1,hidden_layer_2['weights']),hidden_layer_2['biases'])
l2=tf.nn.relu(l2)
l3=tf.add(tf.matmul(l2,hidden_layer_3['weights']),hidden_layer_3['biases'])
l3=tf.nn.relu(l3)
output=tf.add(tf.matmul(l3,output['weights']),output['biases'])
return output
# for splitting out batches of data
class Dataset:
def __init__(self,data):
self._index_in_epoch = 0
self._epochs_completed = 0
self._data = data
self._num_examples = data.shape[0]
pass
@property
def data(self):
return self._data
def next_batch(self,batch_size,shuffle = True):
start = self._index_in_epoch
if start == 0 and self._epochs_completed == 0:
idx = np.arange(0, self._num_examples) # get all possible indexes
np.random.shuffle(idx) # shuffle indexe
self._data = self.data[idx] # get list of `num` random samples
# go to the next batch
if start + batch_size > self._num_examples:
self._epochs_completed += 1
rest_num_examples = self._num_examples - start
data_rest_part = self.data[start:self._num_examples]
idx0 = np.arange(0, self._num_examples) # get all possible indexes
np.random.shuffle(idx0) # shuffle indexes
self._data = self.data[idx0] # get list of `num` random samples
start = 0
self._index_in_epoch = batch_size - rest_num_examples #avoid the case where the #sample != integar times of batch_size
end = self._index_in_epoch
data_new_part = self._data[start:end]
return np.concatenate((data_rest_part, data_new_part), axis=0)
else:
self._index_in_epoch += batch_size
end = self._index_in_epoch
return self._data[start:end]
def traning_neuralNetwork(x_train,y_train):
x=tf.placeholder('float',[None,3072])
y=tf.placeholder('float')
total_epochs=10
total_loss=0
epoch_loss=0
batch_size=200
num_batch = int(np.ceil(48000/batch_size))
prediction=neural_network(x)
cost=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction,labels=y))
optimizer=tf.train.AdamOptimizer().minimize(cost)
temp_x_train_next_batch=Dataset(x_train)
temp_y_train_next_batch=Dataset(y_train)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for epoch in range (total_epochs):
total_loss=0
for _ in range (num_batch):
x_train=temp_x_train_next_batch.next_batch(batch_size)
y_train=temp_y_train_next_batch.next_batch(batch_size)
_,epoch_loss=sess.run([optimizer,cost],feed_dict={x:x_train,y:y_train})
total_loss+=epoch_loss
print('Epoch ',epoch, " loss = ",total_loss)
print("Traning Complete!")
correct=tf.equal(tf.argmax(prediction,1),tf.argmax(y,1))
accuracy=tf.reduce_mean(tf.cast(correct,'float'))
print('accuracy',accuracy.eval({x:x_test,y :y_test}))
- 错误**
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-27-aeb4ef85487e> in <module>()
----> 1 traning_neuralNetwork(x_train,y_train)
<ipython-input-26-7e54c20d2131> in traning_neuralNetwork(x_train, y_train)
102 for _ in range (num_batch):
103 x_train=temp_x_train_next_batch.next_batch(batch_size)
--> 104 y_train=temp_y_train_next_batch.next_batch(batch_size)
105 _,epoch_loss=sess.run([optimizer,cost],feed_dict={x:x_train,y:y_train})
106 total_loss+=epoch_loss
<ipython-input-26-7e54c20d2131> in next_batch(self, batch_size, shuffle)
57 start = self._index_in_epoch
58 if start == 0 and self._epochs_completed == 0:
---> 59 idx = np.arange(0, self._num_examples) # get all possible indexes
60 np.random.shuffle(idx) # shuffle indexe
61 self._data = self.data[idx] # get list of `num` random samples
TypeError: unsupported operand type(s) for /: 'Dimension' and 'int'
3条答案
按热度按时间wgxvkvu91#
也许Tensor不是一个可以计算的值,所以我想在Tensor的末尾加上
.numpy()
可能会起作用。vsnjm48y2#
我认为这里的问题是,您以前以numpy格式操作数据集,但现在使用的是
tf.Variable
或tf.Tensor
等张流格式。这条线
如果data是numpy数组,则返回
int
:但如果数据是tf.variable:
返回
tensor_shape.Dimension
的一个示例,然后np.arrange()
不能在此处使用该示例:因为
np.arrange()
需要int
参数。作为一个快速解决方案,请尝试交换
到
bvhaajcl3#
问题似乎是初始化权重所需的聚类数是由ClusteringLayer作为tf.Dimension对象发送的。为了避免维度对象的问题,请使用int(self.n clusters)。