【问题标题】:TypeError: unsupported operand type(s) for /: 'Dimension' and 'int'TypeError: 不支持的操作数类型 /: 'Dimension' 和 'int'
【发布时间】:2019-01-12 12:08:18
【问题描述】:

希望你们身体健康。尊敬的成员,我正在研究 Cifar10 数据集 并得到 TypeError: unsupported operand type(s) for /: 'Dimension' and 'int' 我对 mnist 数据集(GreyScale)使用相同的算法,但它们没有错误。但现在我使用相同的 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'

【问题讨论】:

  • data.shape[0] 返回一个 Dimension 对象。您应该尝试将其转换为整数。例如int(data.shape[0])
  • 现在他们是切片问题,请参阅下面的错误 形状必须为 1 级,但输入形状为 'strided_slice_400'(操作:'StridedSlice')为 2 级:[49000,10] , [1,49000], [1,49000], [1].
  • 也许this 会帮助你

标签: python numpy tensorflow neural-network deep-learning


【解决方案1】:

也许张量不是一个可以计算的值,所以我想在张量的末尾添加.numpy() 可能会起作用。

【讨论】:

    【解决方案2】:

    我认为这里的问题是您之前以 numpy 格式操作过数据集,但现在使用的是 tensorflow 格式,例如 tf.Variabletf.Tensor

    这一行

    self._num_examples = data.shape[0]
    

    如果 data 是一个 numpy 数组,则返回 int

    np_data = np.random.randn(10,10)
    np_ans = np_data.shape[0]
    print(type(np_ans))
    
    >>> <class 'int'>
    

    但如果数据是 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'>
    

    返回tensor_shape.Dimension 的一个实例,然后np.arrange() 不能在此处使用它:

    ---> 59             idx = np.arange(0, self._num_examples)
    

    因为np.arrange() 需要int 参数。

    作为一个快速修复,尝试交换

    self._num_examples = data.shape[0]
    

    self._num_examples = data.shape[0].value
    

    【讨论】:

      猜你喜欢
      • 2017-12-27
      • 2014-03-31
      • 2012-12-12
      • 2020-02-27
      • 2021-05-23
      • 2016-04-15
      • 2012-11-29
      • 2012-12-31
      • 2015-10-15
      相关资源
      最近更新 更多