tensorflow shuffle_batch和feed_dict错误

时间:2017-10-02 10:38:47

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

这是我的代码的主要部分。 我对功能 shuffle_batch feed_dict 感到困惑。

在下面的代码中,我输入的功能标签是“ list ”。(我也试过“数组“之前。但似乎没关系。”

我想要做的是将我的测试数据(6144,26)和训练数据(1024,13)分批:(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))

1 个答案:

答案 0 :(得分:1)

来自Session.run doc:

  

可选的feed_dict参数允许调用者覆盖   图中张量的值。 feed_dict中的每个键都可以是其中之一   以下类型:

     
      
  • 如果密钥是tf.Tensor,则该值可能是Python标量,字符串,   list或numpy ndarray,可以转换为与该dtype相同的dtype   张量。另外,如果键是tf.placeholder,则表示形状   将检查值是否与占位符兼容。

  •   
  • ...

  •   

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

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

  • 摆脱占位符,并将tf.TFRecordReadertf.train.shuffle_batch结合使用,suggested here。这样你的模型中只有张量,你不需要另外“喂”任何东西。

  • numpy中自行批处理并随机播放数据并输入占位符。这需要just several lines代码,所以我发现它更容易,但两条路径都有效。

还要考虑performance considerations