python - “元组”对象没有属性“火车”
问题描述
我得到的答案是“元组”对象没有“火车”属性。我无法理解这个错误(我正在使用 google colab)。请帮助我,并尽可能地解释更多细节(培训部分)。我的代码在下面。非常感谢提前
%tensorflow_version 1.x
## loading nessecery functions and CIFAR10 dataset
from __future__ import print_function
import tensorflow as tf
from tensorflow.keras.datasets import cifar10
tf.__version__
((train_X, train_y), (test_X, test_y)) = cifar10.load_data()
print(f"train_X: {train_X.shape}, test_X = {test_X.shape}")
cifar10 = cifar10.load_data()
# define placeholder for inputs to network
X = tf.placeholder(tf.float32, [None, 3072])/255.0 # 32x32x3
Y = tf.placeholder(tf.float32, [None, 10])
keep_prob = tf.placeholder(tf.float32)
learning_rate = 0.001
training_epochs =10
batch_size = 30
# weights & bias for nn layers
W = tf.Variable(tf.random_normal([3072, 10]))
b = tf.Variable(tf.random_normal([10]))
hypothesis = tf.matmul(X, W) + b
# define cost/loss & optimizer
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=hypothesis, labels=Y))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
# initialize
sess = tf.Session()
sess.run(tf.global_variables_initializer())
# train my model
for epoch in range(training_epochs):
avg_cost = 0
num_examples = 50000
total_batch = int(num_examples / batch_size)
我的问题在这里
for i in range(total_batch):
batch_xs, batch_ys = cifar10.train.next_batch(batch_size)
feed_dict = {X: batch_xs, Y: batch_ys}
c, _ = sess.run([cost, optimizer], feed_dict=feed_dict)
avg_cost += c / total_batch
print('Epoch:', '%04d' % (epoch + 1), 'cost =', '{:.9f}'.format(avg_cost))
print('Learning Finished!')
# Test model and check accuracy
correct_prediction = tf.equal(tf.argmax(hypothesis, 1), tf.argmax(Y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print('Accuracy:', sess.run(accuracy, feed_dict={X: cifar10.test.images, Y: cifar10.test.labels}))
解决方案
您正在尝试train
从cifar10.load_data()
. 您已经在上一步中正确加载了数据:
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
cifar10.load_data()
是一个数据加载器,它返回数据集的训练集和测试集。
如果你想实现一个next_batch
方法来做上面的事情,你需要定义一个自定义的帮助类,这是很常用的东西。这是一个示例案例。
推荐阅读
- android - 为什么不显示 SD 卡中的图像?
- gatsby - 如何在 Contentful webhook 触发后延迟应用程序重建?
- javascript - JavaScript 通用 Async/Await “去抖动”
- java - Jenkins ssh-node 找不到 Java
- javascript - 空手道框架 - 为什么 Javascript 函数返回具有 NaN 值的数组?
- javascript - 传单路由机
- azure-pipelines - 来宾的新建通知
- docker - 在我的 Kubernetes 基础架构中的 Ingress 之前是否需要有负载均衡器
- django - Django:缓存 wrt url 并反映更改
- java - 将单个 `char` 转换为 `byte[]`,编码感知