gpt4 book ai didi

python - tensorflow shuffle_batch和feed_dict错误

转载 作者:行者123 更新时间:2023-11-30 09:08:21 24 4
gpt4 key购买 nike

这是我的代码的主要部分。我对函数 shuffle_batchfeed_dict 感到困惑。

在下面的代码中,我放入函数中的功能标签是“列表”。(我也尝试过“列表”。 >数组”之前。但似乎并不重要。)

我想要做的是将我的测试数据(6144,26)和训练数据(1024,13)放入batch:(100,26)和(100,13),然后将它们设置为占位符的feed_dict .

我的问题是:

1.函数tf.train.batch_shuffle的输出是张量。但是我不能将张量放入feed_dict中,对吧?

2.当我编译最后两行时,错误说,得到形状 [6144, 26],但想要 [6144] 。我知道这可能是尺寸错误,但我该怎么办修复它。

非常感谢。

import tensorflow as tf
import scipy.io as sio

#import signal matfile
#[('label', (8192, 13), 'double'), ('clipped_DMT', (8192, 26), 'double')]
file = sio.loadmat('DMTsignal.mat')

#get array(clipped_DMT)
data_cDMT = file['clipped_DMT']
#get array(label)
data_label = file['label']


with tf.variable_scope('split_cDMT'):
cDMT_test_list = []
cDMT_training_list = []
for i in range(0,8192):
if i % 4 == 0:
cDMT_test_list.append(data_cDMT[i])
else:
cDMT_training_list.append(data_cDMT[i])


with tf.variable_scope('split_label'):
label_test_list = []
label_training_list = []
for i in range(0,8192):
if i % 4 == 0:
label_test_list.append(data_label[i])
else:
label_training_list.append(data_label[i])


#set parameters
n_features = cDMT_training.shape[1]
n_labels = label_training.shape[1]
learning_rate = 0.8
hidden_1 = 256
hidden_2 = 128
training_steps = 1000
BATCH_SIZE = 100


#set Graph input
with tf.variable_scope('cDMT_Inputs'):
X = tf.placeholder(tf.float32,[None, n_features],name = 'Input_Data')
with tf.variable_scope('labels_Inputs'):
Y = tf.placeholder(tf.float32,[None, n_labels],name = 'Label_Data')


#set variables
#Initialize both W and b as tensors full of zeros
with tf.variable_scope('layerWeights'):
h1 = tf.Variable(tf.random_normal([n_features,hidden_1]))
h2 = tf.Variable(tf.random_normal([hidden_1,hidden_2]))
w_out = tf.Variable(tf.random_normal([hidden_2,n_labels]))

with tf.variable_scope('layerBias'):
b1 = tf.Variable(tf.random_normal([hidden_1]))
b2 = tf.Variable(tf.random_normal([hidden_2]))
b_out = tf.Variable(tf.random_normal([n_labels]))

#create model
def neural_net(x):
layer_1 = tf.add(tf.matmul(x,h1),b1)
layer_2 = tf.nn.relu(tf.add(tf.matmul(layer_1,h2),b2))
out_layer = tf.add(tf.matmul(layer_2,w_out),b_out)
return out_layer

nn_out = neural_net(X)

#loss and optimizer
with tf.variable_scope('Loss'):
loss = tf.reduce_mean(tf.reduce_sum(tf.nn.softmax_cross_entropy_with_logits(logits = nn_out,labels = Y)))
with tf.name_scope('Train'):
optimizer = tf.train.AdamOptimizer(learning_rate).minimize(loss)


with tf.name_scope('Accuracy'):
correct_prediction = tf.equal(tf.argmax(nn_out,1),tf.argmax(Y,1))
#correct_prediction = tf.metrics.accuracy (labels = Y, predictions =nn_out)
acc = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))

# Initialize
init = tf.global_variables_initializer()


# start computing & training
with tf.Session() as sess:
sess.run(init)
for step in range(training_steps):
#set batch
cmt_train_bat,label_train_bat = sess.run(tf.train.shuffle_batch([cDMT_training_list,label_training_list],batch_size = BATCH_SIZE,capacity=50000,min_after_dequeue=10000))
cmt_test_bat,label_test_bat = sess.run(tf.train.shuffle_batch([cDMT_test_list,label_test_list],batch_size = BATCH_SIZE,capacity=50000,min_after_dequeue=10000))

最佳答案

来自Session.run文档:

The optional feed_dict argument allows the caller to override the value of tensors in the graph. Each key in feed_dict can be one of the following types:

  • If the key is a tf.Tensor, the value may be a Python scalar, string, list, or numpy ndarray that can be converted to the same dtype as that tensor. Additionally, if the key is a tf.placeholder, the shape of the value will be checked for compatibility with the placeholder.

  • ...

所以你是对的:对于 XY (它们是占位符),你不能提供张量和 tf.train.shuffle_batch 不适用于占位符。

您可以采用以下两种方法之一:

  • 去掉占位符,并将tf.TFRecordReadertf.train.shuffle_batch结合使用,如 suggested here 。这样,您的模型中就只有张量,而无需额外“提供”任何内容。

  • numpy 中自行批处理和打乱数据,并将其输入占位符。这需要just several lines代码,所以我发现它更容易,尽管两条路径都是有效的。

同时考虑performance considerations .

关于python - tensorflow shuffle_batch和feed_dict错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46523951/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com