Tensorflow keras-输入形状不同于前一层的输出形状

Tensorflow keras-输入形状不同于前一层的输出形状,tensorflow,keras,layer,shapes,Tensorflow,Keras,Layer,Shapes,我将我的模型从Conv2D转换为简单的DNN。我可以以某种方式解决输入/输出形状错误。但现在我的模型给出了疯狂的结果——可怕的损失率和零精度。有人能帮忙吗 """ trying hands on first ML program - 1D model """ import time import numpy as np # import matplotlib.pyplot as plt import pandas as pd import tensorflow as tf import kera

我将我的模型从Conv2D转换为简单的DNN。我可以以某种方式解决输入/输出形状错误。但现在我的模型给出了疯狂的结果——可怕的损失率和零精度。有人能帮忙吗

"""
trying hands on first ML program - 1D model
"""
import time
import numpy as np
# import matplotlib.pyplot as plt
import pandas as pd
import tensorflow as tf
import keras
from keras.models import Model
from keras.layers import Input, Dense, Activation
from keras.utils import to_categorical
from keras.optimizers import RMSprop
from numba import jit, cuda
start = time.time()

data = pd.read_csv(r"Pets_Data.csv", index_col='Date')
# data = data.reset_index(drop=False)
target = "Hippo"
filter = (data["Cats"] > 0) & (data[target] > 0)
total = data.where(filter).dropna().shape[0]
x_df = data.where(filter).dropna()
y_df = pd.DataFrame(data=x_df, columns=[target], copy=True)
x_df = x_df.drop(target, axis=1)
# input_shape = x_df.shape[1]

Train_ratio = 0.8
train_x = x_df[:int(total * Train_ratio)]
train_y = y_df[:int(total * Train_ratio)]

x = np.array(train_x, copy='True', order='K').flatten(order='C')
y = np.array(train_y, copy='True', order='K').flatten(order='C')

# tr_x = to_categorical(x,959)
# tr_y = to_categorical(y,959)

test_x = np.array(x_df[int(total * Train_ratio):], copy='True', order='K')#.flatten(order='C')
test_y = np.array(y_df[int(total * Train_ratio):], copy='True', order='K')#.flatten(order='C')

# x_test_new = to_categorical(test_x, 240)
# y_test_new = to_categorical(test_y,240)

inputs = Input(shape=(43,))
output_1 = Dense(256, activation='relu')(inputs)
output_2 = Dense(128, activation='relu')(output_1)
predictions = Dense(959, activation ='softmax')(output_2)

# Build the model
model = Model(inputs=inputs, outputs=predictions)
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) #loss='mean_squared_error'
model.summary()

jit(target='cuda')
# Fit the model
model.fit(train_x, train_y, epochs=100, batch_size=43, verbose=1)  # starts training
end = time.time()
print(f"time taken : {end-start:.4f} seconds.")

# Evaluate model
score = model.evaluate(test_x, test_y, batch_size=None)
print(f"loss on test: {score[0]*100:>6.2f}%, accuracy {score[1]*100:>6.2f}% ")
endtime = time.time()
print(f"time taken : {endtime-start:>6,.4f} seconds")
'''


图层(类型)输出形状参数# 输入_1(输入层)(无,43)0


密集型_1(密集型)(无,256)11264


致密(致密)(无,128)32896


密集型_3(密集型)(无,959)123711 总参数:167871 可培训参数:167871 不可训练参数:0 ... 959/959[==============================]-0s69us/步-损耗:5.9462-精度:0.0010 纪元41/50 959/959[=======================================]0秒66us/步-损耗:5.9270-精度:0.0010 纪元42/50 959/959[=============================================-0s 68us/步-损耗:5.9081-精度:0.0010 纪元43/50 959/959[=================================]-0s68us/步-损耗:5.8894-精度:0.0010 纪元44/50 959/959[=================================]-0s 68us/步-损耗:5.8709-精度:0.0010 纪元45/50 959/959[=============================================-0s68us/步-损耗:5.8526-精度:0.0010 纪元46/50 959/959[=============================================-0s 67us/步-损耗:5.8346-精度:0.0010 纪元47/50 959/959[==========================================-0s64us/步-损耗:5.8168-精度:0.0010 纪元48/50 959/959[=================================]-0s70us/步-损耗:5.7992-精度:0.0010 纪元49/50 959/959[==========================================]0秒63us/步-损耗:5.7818-精度:0.0010 纪元50/50 959/959[=============================================-0s64us/步-损耗:5.7646-精度:0.0010 所用时间:3.9185秒。 240/240[=======================================]-0s 154us/步 试验损失:595.13%,准确度0.00%
所用时间:3.9664秒

您说您只有数字数据(1199行,每个行有43个特征),没有图像。但是在你的模型中你使用二维卷积,这通常用于图像。还可以将第一层的输入_形状指定为839x43。我明白了,因为839是1199*0.7层。但这意味着您的模型将看到您作为一个图像输入,而不是839个不同的实例,每个实例具有43个特征。您可能喜欢使用一维卷积或仅使用密集层来完成任务。

谢谢,如果您所说的是真的,那么我的模型很糟糕。我想在按日期索引的1199*43个数字元素之间建立回归关系。43表示类别,1199表示每个类别达到数字的日期。我想我还得等很久才能把它弄对。再次感谢!如果你能建议修改代码,那就太棒了,但我也会尝试自己去做。你也可以分享一些我试图实现的已经讨论过的链接吗?很可能一个简单的DNN就能完成这项工作。以keras文档为例,我刚刚注意到你的num_类是1199,你编译了两次模型,一次是交叉熵错误,一次是mse。你喜欢分类还是回归?回归。我真的是一个新手,正在努力学习人工智能。这是我第一次尝试,即使是基本的技术问题,我也回答不了。过了一段时间,我肯定能做到。非常感谢你的支持。顺便说一句-我很想了解更多关于你的信息-我是一名金融专业人士,学习人工智能只足以理解运动部件的“内容”,这样当我与编写代码的专家合作时,我就能理解他们的语言。让我知道我们是否可以单独连接。你能看看我修改后的代码,上面有问题的编辑吗?