![](/img/trans.png)
[英]TensorFlow: Incompatible shapes: [100,155] vs. [128,155] when combining CNN and LSTM
[英]TensorFlow Incompatible shapes: [870] vs. [2] for CNN
我是 TensorFLow 的新手,在使用 MINST 数据集后,我想尝试自己的。 在尝试了不同的事情之后,我认为我的数据集可以正常工作,但在训练时仍然出现错误。 我认为我的输入有问题,我不知道是什么。 我得到了一个文件夹中的数据,其中包含 CSV。
我得到的错误如下:
InvalidArgumentError: Incompatible shapes: [870] vs. [2]
[[node Equal (defined at <ipython-input-13-9f1594aed79a>:111) ]][Op:__inference_train_function_9409]
Function call stack:
train_function
这就是代码
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
import tensorflow as tf
import pandas as pd
from tensorflow import keras
from tensorflow.keras import datasets, layers, models
import matplotlib.pyplot as plt
import numpy as np
import io
directory = 'E:/Diplom/Datasets/DFDC/dfdc_train_part_4_png/'
from google.colab import files
uploaded = files.upload()
df = pd.read_csv(io.StringIO(uploaded['3.csv'].decode('utf-8')))
file_paths = df['_key'].values
print(file_paths)
print(1)
labels = df['label'].values
print(labels.size)
print(labels)
print(labels[1])
länge = labels.size
for i in range(länge):
if (labels[i] == "FAKE"):
labels[int(i)] = "0"
else:
labels[int(i)] = "1"
print(labels)
tf.strings.to_number(labels, tf.float32)
print(labels)
print(2)
filenames = tf.constant(file_paths)
print(filenames)
print(3)
labels = tf.constant(labels)
tf.strings.to_number(labels, tf.float32)
print(labels)
print(4)
# step 2: create a dataset returning slices of `filenames`
dataset = tf.data.Dataset.from_tensor_slices((filenames,labels))
print(dataset)
tf.strings.to_number(labels, tf.float32)
print(5)
# step 3: parse every image in the dataset using `map`
def _parse_function(filename, label):
image_string = tf.io.read_file(filename)
print(image_string)
print(6)
image_decoded = tf.io.decode_png(image_string, channels=3)
print(image_decoded)
print(image_decoded.shape)
print(7)
image = tf.cast(image_decoded, tf.float32)
print(image)
print(8)
label = int(label)
tf.image.resize(image, [250,250])
print(image)
print(9)
return image, label
dataset = dataset.map(_parse_function)
print(dataset)
print(10)
dataset = dataset.batch(2)
# Erstellung des Models
model = models.Sequential()
model.add(layers.Conv2D(32, (11, 11), activation='relu', input_shape=(256, 256, 3)))
model.add(layers.MaxPooling2D(5,5))
model.add(layers.Conv2D(64, (9, 9), activation='relu',))
model.add(layers.MaxPooling2D(4,4))
model.add(layers.Conv2D(64, (7, 7), activation='relu',))
model.add(layers.MaxPooling2D(3,3))
#Vollverbundene Schichten
model.add(layers.Flatten())
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='sigmoid'))
model.add(layers.Dense(32, activation='sigmoid'))
model.add(layers.Dense(2))
# Kompilieren
model.compile(optimizer='adam',
# lossfunction
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
# Moddel darstellung
model.summary()
model.fit(dataset, epochs=1, verbose=1)
我看到多个错误:
tf.image.resize(image, [250,250])
和
model.add(layers.Conv2D(32, (11, 11), activation='relu', input_shape=(256, 256, 3))
输入形状应为图像height x width x channel(s)
。 如果您使用灰度图像,则通道为 1,或者使用 RGB 图像,则通道应为 3。
model.add(layers.Flatten())
压扁一次就足够了。
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(32, activation='relu'))
model.add(layers.Dense(num_of_classes, activation = 'softmax'))
Sigmoid 在隐藏层中不常用,因此应该像在之前的层中那样将其更改为'relu'
。 由于是多类分类,output 层应该有'softmax'
作为激活。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.