Reputation: 1547
Hope you guys are in good health. Respected members I am working on Cifar10 dataset and getting TypeError: unsupported operand type(s) for /: 'Dimension' and 'int' I use same algorithm for mnist dataset(GreyScale) but their is no error. But now I am using same algorithm of tensorflow for training on Cifar10(rgb). Is this error occurs because of (rgb) dataset?I am sharing my code please have a look Thank you
Code
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}))
error
---------------------------------------------------------------------------
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'
Upvotes: 2
Views: 10310
Reputation: 921
I believe the problem here is that you have previously manipulated your data sets in numpy formats, but now are using tensorflow foormats such as tf.Variable
or tf.Tensor
.
This line
self._num_examples = data.shape[0]
returns an int
if data is a numpy array:
np_data = np.random.randn(10,10)
np_ans = np_data.shape[0]
print(type(np_ans))
>>> <class 'int'>
but if data is a tf.Variable :
tf_data = tf.Variable(np.random.randn(10,10), dtype=tf.float32)
tf_ans = tf_data.shape[0]
print(type(tf_ans))
>>> <class 'tensorflow.python.framework.tensor_shape.Dimension'>
an instance of tensor_shape.Dimension
is returned which can then not be used by np.arrange()
here:
---> 59 idx = np.arange(0, self._num_examples)
because np.arrange()
requires int
arguments.
As a quick fix, try swapping
self._num_examples = data.shape[0]
to
self._num_examples = data.shape[0].value
Upvotes: 1
Reputation: 11
Maybe tensor not a value that can be calculated, so I guess adding .numpy()
to the end of the tensor might work.
Upvotes: 1