相关疑难解决方法(0)

TensorFlow:不兼容的形状:[100,155] 与 [128,155] 结合 CNN 和 LSTM 时

问题

尝试为音频回归任务堆叠 Conv -> Lstm -> 完全连接层时出现不兼容的形状错误。我不知道为什么我会收到我收到的错误 - 图表构建良好然后抛出错误 - 任何人都可以帮忙吗?

代码

lstm_num_hidden = 128
lstm_number_layers = 3
x = tf.placeholder(tf.float32, [None, 1024])
y = tf.placeholder(tf.float32, [None, 155])
keep_probability = tf.placeholder(tf.float32)

def conv2d(x, weights):
    return tf.nn.conv2d(x, weights, strides=[1, 1, 1, 1], padding='SAME')

x_spectrogram = tf.reshape(x, [-1, 32, 32, 1])

conv1_weights = tf.Variable(tf.truncated_normal([5, 5, 1, 32], stddev=0.1))
conv1_bias = tf.Variable(tf.constant(0.1, shape=[32]))
conv1_hidden = tf.nn.relu(conv2d(x_spectrogram, conv1_weights) + conv1_bias)
conv1_pooling = tf.nn.max_pool(conv1_hidden, ksize=[1, 2, 2, 1],strides=[1, 2, 2, 1], padding='SAME')

conv2_weights = tf.Variable(tf.truncated_normal([5, …
Run Code Online (Sandbox Code Playgroud)

python neural-network deep-learning tensorflow

6
推荐指数
1
解决办法
3万
查看次数

张量流(使用 Keras)中“InvalidArgumentError: Incompatible shape: [10,2] vs. [10]”的原因是什么?

我正在尝试使用 CNN 使用 Tensorflow 和 Keras 进行对象检测。我对此很陌生,所以我使用教程作为指南,但有我自己的设置和其他一些东西。我得到的错误是 Tensorflow 的形状与 [x,2] 与 [x] 不兼容,其中 x 是我拥有的任意数量的训练图像,2 是类的数量。我仅使用少量图像进行测试,但我很确定这不是问题所在?

我尝试了不同倍数的训练图像但没有成功,我查看了 model.summary() 以查看模型是否完全按照我想要的方式布局。此外,我还打印了训练图像的形状及其标签,它们看起来是正确的。

图像大小为 28 x 28 像素,平面大小为 784,完整形状为 (28,28,1),1 是通道数(灰度)。我只有两个班级,总共只有 10 个训练图像(如果认为这是问题所在,我可以得到更多)。

model = Sequential()

model.add(InputLayer(input_shape=(img_size_flat,)))

model.add(Reshape(img_shape_full))

model.add(Conv2D(kernel_size=5, strides=1, filters=16, padding='same',
                 activation='relu', name='layer_conv1'))
model.add(MaxPooling2D(pool_size=2, strides=2))

model.add(Conv2D(kernel_size=5, strides=1, filters=36, padding='same',
                 activation='relu', name='layer_conv2'))
model.add(MaxPooling2D(pool_size=2, strides=2))

model.add(Flatten())

model.add(Dense(128, activation='relu'))

model.add(Dense(num_classes, activation='softmax'))

from tensorflow.python.keras.optimizers import Adam
optimizer = Adam(lr=1e-3)

model.compile(optimizer=optimizer,
              loss='categorical_crossentropy',
              metrics=['accuracy'])

from tensorflow.python.keras.utils import to_categorical
model.fit(x=data.train,
    y=to_categorical(data.train_labels),
    batch_size=128, epochs=1)
Run Code Online (Sandbox Code Playgroud)

我在标签上使用 to_categorical() 只是因为它们以某种方式被转换为整数。我检查了他们是否保留了正确的值等。

我打印了模型摘要以检查布局:

_________________________________________________________________
Layer (type) …
Run Code Online (Sandbox Code Playgroud)

object-detection computer-vision python-3.x keras tensorflow

5
推荐指数
1
解决办法
7078
查看次数

Tutorialorflow中的Tensorflow不兼容的形状错误

我一直试图从Tensorflow教程创建卷积网络,但我遇到了麻烦.出于某种原因,我遇到的错误是y_conv的大小比y_的大小大4倍,我不明白为什么.我发现了这个问题,但它似乎与我的问题不同,尽管它看起来很相似.

要清楚,下面代码中的批处理大小是50,但它出现的错误是

tensorflow.python.framework.errors.InvalidArgumentError:不兼容的形状:[200]与[50]

当我将批量大小更改为10时,我得到了

tensorflow.python.framework.errors.InvalidArgumentError:不兼容的形状:[40]与[10]

所以它与批量大小有某种关系,但我无法弄明白.谁能告诉我这段代码有什么问题?它与上面链接的教程非常相似.

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)

import tensorflow as tf
sess = tf.InteractiveSession()

def weight_variable(shape):
  initial = tf.truncated_normal(shape, stddev=0.1)
  return tf.Variable(initial)

def bias_variable(shape):
  initial = tf.constant(0.1, shape=shape)
  return tf.Variable(initial)

def conv2d(x, W):
  return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')

def max_pool_2x2(x):
  return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides = [1, 2, 2, 1], padding='SAME')

x = tf.placeholder("float", shape=[None, 784])
y_ = tf.placeholder("float", shape=[None, 10])

w_conv1 …
Run Code Online (Sandbox Code Playgroud)

tensorflow

0
推荐指数
1
解决办法
7494
查看次数