【问题标题】:Tensorflow doesn't train: 'DataFrame' objects are mutable, thus they cannot be hashedTensorflow 不训练:“DataFrame”对象是可变的,因此它们不能被散列
【发布时间】:2019-07-14 20:12:09
【问题描述】:

我想在 kaggle 数据集“房价”上使用tensorflow(但没有Keras,在Keras 上,我得到它的工作)构建和训练神经网络。我使用 Python,除了实际训练之外,我的代码运行良好。但是,在训练时,我要么没有错误(但它没有训练),要么得到TypeError: 'DataFrame' objects are mutable, thus they cannot be hashed

我在 ipynotebook 中在 Google 的 colab 上运行脚本,我认为主要问题在于输入 feed_dict。但是,我不知道这里有什么问题。 batch_X 包含 100x10 特征,batch_Y 具有 100 标签。我想这可能是关键的片段:

train_data = { X: batch_X, Y_:batch_Y }

train_data 是我提供给 sess.run(train_step, feed_dict=train_data") 的内容

这是我的代码:https://colab.research.google.com/drive/1qabmzzicZVu7v72Be8kljM1pUaglb1bY

# train and train_normalized are the training data set (DataFrame)
# train_labels_normalized are the labels only

#Start session:
with tf.Session() as sess:
  sess.run(init)

  possible_indeces = list(range(0, train.shape[0]))
  iterations = 1000
  batch_size = 100

  for step in range(0, iterations):
    #draw batch indeces:
    batch_indeces = random.sample(possible_indeces, batch_size)
    #get features and respective labels
    batch_X = np.array(train_normalized.iloc[batch_indeces])
    batch_Y = np.array(train_labels_normalized.iloc[batch_indeces])

    train_data = { X: batch_X, Y_: batch_Y}

    sess.run(train_step, feed_dict=train_data)

我所希望的是它会运行几分钟并返回优化权重(2 隐藏层,每个48 节点)允许我进行预测。但是,它只是跳过上面的代码或抛出错误。

有人知道出了什么问题吗?

TypeError Traceback (most recent call last)
<ipython-input-536-79506f90a868> in <module>()
     13     batch_Y = p.array(train_labels_normalized.iloc[batch_indeces])
     14 
---> 15     train_data = { X: batch_X, Y_: batch_Y}
     16 
     17     sess.run(train_step, feed_dict=train_data)

  /usr/local/lib/python3.6/dist-packages/pandas/core/generic.py in __hash__(self)

   1814  def __hash__(self):
   1815  raise TypeError('{0!r} objects are mutable, thus they cannot be'
-> 1816     ' hashed'.format(self.__class__.__name__))
   1817
   1818     def __iter__(self):

  TypeError: 'DataFrame' objects are mutable, thus they cannot be hashed

【问题讨论】:

  • 您的代码适用于我的 Colab 副本?你能编辑你的堆栈跟踪吗?它很难阅读。
  • 感谢您的关注,@SıddıkAçıl。出于某种原因,它有时只会出现该错误。我现在重新格式化了回溯并在 Colab 中添加了一些额外的输出。额外的输出清楚地表明模型没有像我希望的那样学习。但是,它确实会迭代。也许优化器和损失函数不能一起工作?我不知道... O_ô 非常感谢任何帮助。

标签: tensorflow training-data kaggle


【解决方案1】:

问题源于您的第七(测试)步骤。

#Set X to the test data
X = test_normalized.astype(np.float32)
print(type(X)) # **<class 'pandas.core.frame.DataFrame'>**
Y1 = tf.nn.sigmoid(tf.matmul(X, W1))
Y2 = tf.nn.sigmoid(tf.matmul(Y1, W2))
Y3 = tf.matmul(Y2, W3)

您正在将 X 设置为 DataFrame。在第一次运行时,这不会影响任何事情。但是,当你在第七步之后运行第六步时,你会遇到这个问题,因为你已经覆盖了X 的内容。

尝试将X 更改为X_

X_ = test_normalized.astype(np.float32)
Y1 = tf.nn.sigmoid(tf.matmul(X_, W1))

另外,您的最终评估不起作用。将其发送到tf.Session

【讨论】:

  • 非常感谢! @Sıddık Açıl 这确实解决了出现错误的问题。 (:我还在当前版本的 Colab 中更改了 eval 的代码。但是,输出表明,代码根本没有学习......你知道为什么会这样吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多