Error when checking model target: expected activation_2 to have shape (None, 10) but got array with

本文探讨了在使用reshape层调整输出维度时,确保GTlabel(地面真实标签)与之匹配的重要性。当reshape层的输出为10长度,而标签仍保持1长度时,会出现维度不匹配的问题,导致训练过程无法正常进行。
摘要由CSDN通过智能技术生成

我遇到了这个问题,因为我在用reshape层后,忘了改我的GT label的尺寸。

reshape最终的输出是10长度的,但是标签用的还是1长度的

就是(0,0,1,0,0,0,0,...)与3的不匹配

Error when checking target: expected add_6 to have shape (3, 3) but got array with shape (3, 1)是报错,源代码为import pandas as pd import numpy as np import matplotlib.pyplot as plt from keras.models import Model, Input from keras.layers import Conv1D, BatchNormalization, Activation, Add, Flatten, Dense from keras.optimizers import Adam # 读取CSV文件 data = pd.read_csv("3c_left_1-6.csv", header=None) # 将数据转换为Numpy数组 data = data.values # 定义输入形状 input_shape = (data.shape[1], 1) # 定义深度残差网络 def residual_network(inputs): # 第一层卷积层 x = Conv1D(32, 3, padding="same")(inputs) x = BatchNormalization()(x) x = Activation("relu")(x) # 残差块 for i in range(5): y = Conv1D(32, 3, padding="same")(x) y = BatchNormalization()(y) y = Activation("relu")(y) y = Conv1D(32, 3, padding="same")(y) y = BatchNormalization()(y) y = Add()([x, y]) x = Activation("relu")(y) # 全局池化层和全连接层 x = Flatten()(x) x = Dense(128, activation="relu")(x) x = Dense(data.shape[1], activation="linear")(x) outputs = Add()([x, inputs]) return outputs # 构建模型 inputs = Input(shape=input_shape) outputs = residual_network(inputs) model = Model(inputs=inputs, outputs=outputs) # 编译模型 model.compile(loss="mean_squared_error", optimizer=Adam()) # 训练模型 model.fit(data[..., np.newaxis], data[..., np.newaxis], epochs=100) # 预测数据 predicted_data = model.predict(data[..., np.newaxis]) # 可视化去噪前后的数据 fig, axs = plt.subplots(3, 1, figsize=(12, 8)) for i in range(3): axs[i].plot(data[:, i], label="Original Signal") axs[i].plot(predicted_data[:, i], label="Denoised Signal") axs[i].legend() plt.savefig("denoised_signal.png") # 将去噪后的数据保存为CSV文件 df = pd.DataFrame(predicted_data, columns=["x", "y", "z"]) df.to_csv("denoised_data.csv", index=False)
05-10
import pandas as pd import numpy as np import matplotlib.pyplot as plt from keras.models import Model, Input from keras.layers import Conv1D, BatchNormalization, Activation, Add, Flatten, Dense from keras.optimizers import Adam # 读取CSV文件 data = pd.read_csv("3c_left_1-6.csv", header=None) # 将数据转换为Numpy数组 data = data.values # 定义输入形状 input_shape = (data.shape[1], 1) # 定义深度残差网络 def residual_network(inputs): # 第一层卷积层 x = Conv1D(32, 3, padding="same")(inputs) x = BatchNormalization()(x) x = Activation("relu")(x) # 残差块 for i in range(5): y = Conv1D(32, 3, padding="same")(x) y = BatchNormalization()(y) y = Activation("relu")(y) y = Conv1D(32, 3, padding="same")(y) y = BatchNormalization()(y) y = Add()([x, y]) x = Activation("relu")(y) # 全局池化层和全连接层 x = Flatten()(x) x = Dense(128, activation="relu")(x) x = Dense(data.shape[1], activation="linear")(x) outputs = Add()([x, inputs]) return outputs # 构建模型 inputs = Input(shape=input_shape) outputs = residual_network(inputs) model = Model(inputs=inputs, outputs=outputs) # 编译模型 model.compile(loss="mean_squared_error", optimizer=Adam()) # 训练模型 model.fit(data[..., np.newaxis], np.squeeze(data), epochs=100) # 预测数据 predicted_data = model.predict(data[..., np.newaxis]) predicted_data = np.squeeze(predicted_data) # 可视化去噪前后的数据 fig, axs = plt.subplots(3, 1, figsize=(12, 8)) for i in range(3): axs[i].plot(data[:, i], label="Original Signal") axs[i].plot(predicted_data[:, i], label="Denoised Signal") axs[i].legend() plt.savefig("denoised_signal.png") # 将去噪后的数据保存为CSV文件 df = pd.DataFrame(predicted_data, columns=["x", "y", "z"]) df.to_csv("denoised_data.csv", index=False)报错为Traceback (most recent call last): File "G:\project2\main.py", line 51, in <module> model.fit(data[..., np.newaxis], np.squeeze(data), epochs=100) File "G:\python\envs\tensorflow\lib\site-packages\keras\engine\training.py", line 1154, in fit batch_size=batch_size) File "G:\python\envs\tensorflow\lib\site-packages\keras\engine\training.py", line 621, in _standardize_user_data exception_prefix='target') File "G:\python\envs\tensorflow\lib\site-packages\keras\engine\training_utils.py", line 135, in standardize_input_data 'with shape ' + str(data_shape)) ValueError: Error when checking target: expected add_6 to have 3 dimensions, but got array with shape (575206, 3)
05-10
import pandas as pd import numpy as np import matplotlib.pyplot as plt from keras.models import Model, Input from keras.layers import Conv1D, BatchNormalization, Activation, Add, Flatten, Dense from keras.optimizers import Adam # 读取CSV文件 data = pd.read_csv("3c_left_1-6.csv", header=None) # 将数据转换为Numpy数组 data = data.values # 定义输入形状 input_shape = (data.shape[1], 1) # 定义深度残差网络 def residual_network(inputs): # 第一层卷积层 x = Conv1D(32, 3, padding="same")(inputs) x = BatchNormalization()(x) x = Activation("relu")(x) # 残差块 for i in range(5): y = Conv1D(32, 3, padding="same")(x) y = BatchNormalization()(y) y = Activation("relu")(y) y = Conv1D(32, 3, padding="same")(y) y = BatchNormalization()(y) y = Add()([x, y]) x = Activation("relu")(y) # 全局池化层和全连接层 x = Flatten()(x) x = Dense(128, activation="relu")(x) x = Dense(data.shape[1], activation="linear")(x) outputs = Add()([x, inputs]) return outputs # 构建模型 inputs = Input(shape=input_shape) outputs = residual_network(inputs) model = Model(inputs=inputs, outputs=outputs) # 编译模型 model.compile(loss="mean_squared_error", optimizer=Adam()) # 训练模型 model.fit(data[..., np.newaxis], data[..., np.newaxis, np.newaxis], epochs=100) # 预测数据 predicted_data = model.predict(data[..., np.newaxis]) predicted_data = np.squeeze(predicted_data) # 可视化去噪前后的数据 fig, axs = plt.subplots(3, 1, figsize=(12, 8)) for i in range(3): axs[i].plot(data[:, i], label="Original Signal") axs[i].plot(predicted_data[:, i], label="Denoised Signal") axs[i].legend() plt.savefig("denoised_signal.png") # 将去噪后的数据保存为CSV文件 df = pd.DataFrame(predicted_data, columns=["x", "y", "z"]) df.to_csv("denoised_data.csv", index=False)报错为Traceback (most recent call last): File "G:\project2\main.py", line 51, in <module> model.fit(data[..., np.newaxis], data[..., np.newaxis, np.newaxis], epochs=100) File "G:\python\envs\tensorflow\lib\site-packages\keras\engine\training.py", line 1154, in fit batch_size=batch_size) File "G:\python\envs\tensorflow\lib\site-packages\keras\engine\training.py", line 621, in _standardize_user_data exception_prefix='target') File "G:\python\envs\tensorflow\lib\site-packages\keras\engine\training_utils.py", line 135, in standardize_input_data 'with shape ' + str(data_shape)) ValueError: Error when checking target: expected add_6 to have 3 dimensions, but got array with shape (575206, 3, 1, 1)
最新发布
05-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值