Python 使用tensorflow进行训练时形状不正确

Python 使用tensorflow进行训练时形状不正确,python,tensorflow,conv-neural-network,convolution,Python,Tensorflow,Conv Neural Network,Convolution,我一直在关注一个卷积神经网络教程,使用YouTube视频中的Tensorflow和MNIST数据集上的Tensorflow CNN教程。我用这些教程创建了自己的CNN音频数据。我们的目标是通过CNN识别33位发言者的声音。数据已经被争论过了,所以测试集的形状是(8404,1500,1),这样就可以应用卷积。每段音频长度为500,测试集中有8404个样本。我的问题是在训练阶段。我得到以下错误: ValueError:无法为具有形状“(?,500)”的张量“占位符:0”提供形状(128,1500,1

我一直在关注一个卷积神经网络教程,使用YouTube视频中的Tensorflow和MNIST数据集上的Tensorflow CNN教程。我用这些教程创建了自己的CNN音频数据。我们的目标是通过CNN识别33位发言者的声音。数据已经被争论过了,所以测试集的形状是(8404,1500,1),这样就可以应用卷积。每段音频长度为500,测试集中有8404个样本。我的问题是在训练阶段。我得到以下错误:

ValueError:无法为具有形状“(?,500)”的张量“占位符:0”提供形状(128,1500,1)的值

我在谷歌上搜索了这个值错误,人们通过将批次x重塑为预期尺寸解决了这个问题。因此,我尝试了以下代码行:
batch\ux=np.重塑(batch\ux,[-1500])

我这次整形运气不好。有人研究过这个问题吗?下面是代码

import numpy as np
import tensorflow as tf
npzfile = np.load('saved_data_file_33.npz')

train_segs = npzfile['train_segs']              # Seg train data
train_labels = npzfile['train_labels']          # train labels
train_labels_1h = npzfile['train_labels_1h']    # One hot encoding for training data
epochs = 1
batch_size = 128
learning_rate = 0.01
classes = len(train_labels_1h[0,:])  # 33 classes
seg_size = len(test_segs[0,0,:,0])   # 500 long

x = tf.placeholder(tf.float32, [None, seg_size])
y = tf.placeholder(tf.float32)

# This section is initializing the weights and biases of each hidden layer and output layer with random values.
# These values are stores in a dict for easy access.
weights = {"conv1" : tf.Variable(tf.random_normal([5, 5, 1, 32])),
            "conv2": tf.Variable(tf.random_normal([5, 5, 32, 64])),
            "fc_layer": tf.Variable(tf.random_normal([1*125*64, 1024])),
            "output": tf.Variable(tf.random_normal([1024, classes]))
            }
biases = { "b_c1" : tf.Variable(tf.random_normal([32])),
            "b_c2" : tf.Variable(tf.random_normal([64])),
            "b_fc" : tf.Variable(tf.random_normal([1024])),
            "output": tf.Variable(tf.random_normal([classes]))
            }

reshapedX = tf.reshape(x, [-1, 1, 500, 1])

conv1 = tf.nn.conv2d(reshapedX, weights["conv1"], strides = [1, 1, 1, 1], padding = "SAME")
conv1 = tf.nn.relu(conv1 + biases["b_c1"])
conv1 = tf.nn.max_pool(conv1, ksize = [1, 1, 2, 1], strides = [1, 1, 2, 1], padding = "SAME")

conv2 = tf.nn.conv2d(conv1, weights["conv2"], strides = [1, 1, 1, 1], padding = "SAME")
conv2 = tf.nn.relu(conv2 + biases["b_c2"])
conv2 = tf.nn.max_pool(conv2, ksize = [1, 1, 2, 1], strides = [1, 1, 2, 1], padding = "SAME")

fc = tf.reshape(conv2, [-1, 1*125*64])
fc = tf.nn.relu(tf.matmul(fc, weights["fc_layer"]) + biases["b_fc"])

output_layer = tf.matmul(fc, weights["output"]) + biases["output"]

cross_entropy = tf.reduce_mean(
    tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=output_layer))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(output_layer, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

with tf.Session() as sess:
  sess.run(tf.global_variables_initializer())
  for i in range(epochs):
      j = 0
      while j < len(train_segs):
          start = i
          end = i + batch_size
          batch_x = np.array(train_segs[start:end])
          batch_y = np.array(train_labels[start:end])
          #batch_x = np.reshape(batch_x, [-1, 500]) # reshape for x input. s
          train_accuracy = accuracy.eval(feed_dict={x: batch_x, y: batch_y})
          print('step %d, training accuracy %g' % (i, train_accuracy))
          train_step.run(feed_dict={x: batch_x, y: batch_y})

  print('test accuracy %g' % accuracy.eval(feed_dict={
      x: train_segs, y: train_labels}))
将numpy导入为np
导入tensorflow作为tf
npzfile=np.load('saved_data_file_33.npz'))
列车间隔=NPZ文件[“列车间隔”]#间隔列车数据
列车标签=NPZ文件[“列车标签”]#列车标签
列车标签\u 1h=npzfile['train\u labels\u 1h']#一个用于训练数据的热编码
时代=1
批量大小=128
学习率=0.01
classes=len(列车标签_1h[0,:])#33类
seg_size=len(测试segs[0,0,:,0])#500长
x=tf.placeholder(tf.float32,[None,seg_size])
y=tf.placeholder(tf.float32)
#本节使用随机值初始化每个隐藏层和输出层的权重和偏差。
#这些值存储在dict中,便于访问。
权重={“conv1”:tf.Variable(tf.random_normal([5,5,1,32]),
“conv2”:tf.变量(tf.random_normal([5,5,32,64]),
“fc_层”:tf.变量(tf.随机_法线([1*125*641024]),
“输出”:tf.Variable(tf.random_normal([1024,classes]))
}
偏差={“b_c1”:tf.变量(tf.随机_正态([32])),
“b_c2”:tf.变量(tf.随机_正常值([64]),
“b_fc”:tf.变量(tf.随机正常值([1024]),
“输出”:tf.Variable(tf.random_normal([classes]))
}
重塑(x,[-1500,1])
conv1=tf.nn.conv2d(重塑X,权重[“conv1”],步幅=[1,1,1,1],padding=“相同”)
conv1=tf.nn.relu(conv1+偏差[“b_c1”])
conv1=tf.nn.max_pool(conv1,ksize=[1,1,2,1],步长=[1,1,2,1],padding=“相同”)
conv2=tf.nn.conv2d(conv1,权重[“conv2”],步幅=[1,1,1,1],padding=“相同”)
conv2=tf.nn.relu(conv2+偏差[“b_c2”])
conv2=tf.nn.max_pool(conv2,ksize=[1,1,2,1],步长=[1,1,2,1],padding=“相同”)
fc=tf.重塑(conv2,[-1,1*125*64])
fc=tf.nn.relu(tf.matmul(fc,权重[“fc_层])+偏差[“b_fc”])
输出层=tf.matmul(fc,权重[“输出”])+偏差[“输出”]
交叉熵=tf.reduce\u平均值(
tf.nn.softmax_cross_entropy_与_logits(labels=y,logits=output\u layer))
列车步长=tf.列车AdamOptimizer(1e-4).最小化(交叉熵)
正确的预测=tf.equal(tf.argmax(输出层,1),tf.argmax(y,1))
准确度=tf.reduce_平均值(tf.cast(正确的预测,tf.float32))
使用tf.Session()作为sess:
sess.run(tf.global\u variables\u initializer())
对于范围内的i(历元):
j=0
当j
看起来您想从
列车分段中删除尺寸为1的尺寸。您可以使用
train\u segs=np.挤压(train\u segs)
进行此操作


另外,我认为您在
np.reformate
中使用了错误的括号,因此
np.reformate(batch_x,(-1500))
可能会起作用。一般来说,您需要小心使用
重塑
函数,因为元素的顺序可能不会像您期望的那样结束。

感谢您对numpy重塑格式的了解。谢谢你的建议。那肯定是我错过的东西。然而,我的代码中最大的错误是,我没有通过1热编码标签!非常感谢你的帮助,我非常感激。