tensorflow - Tensorflow 不训练:“DataFrame”对象是可变的,因此它们不能被散列
问题描述
我想在 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
解决方案
问题源于您的第七(测试)步骤。
#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
.
推荐阅读
- laravel - 如何更改 vuejs laravel 中的选项标签
- django - Django 和 postgresql 模式
- html - AngularJS为每个结果属性创建新表
- html - 使用 Eclipse 和 HTML5 doctype 在 facelets 中打开未关闭标签的警告
- git - git lfs 跟踪文件类型的大小大于
- python - Odoo 10 群发邮件配置退回
- python - 如何在同一个图中用不同的键绘制字典?
- java - 无法在项目 produit-webapp 上执行目标 org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile)
- angularjs - 如何在 adal-angular 的 httpinterceptor 发送的承载中添加声明
- ruby-on-rails - 在 Rails 的子记录中安装外键时遇到问题