我将模型从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")
'''
input_1(InputLayer)(无,43)0
dense_1(密集)(无,256)11264
dense_2(密集)(无,128)32896
总参数:167,871 可调参数:167,871 不可训练的参数:0 ... 959/959 [==============================]-0s 69us / step-损耗:5.9462-精度:0.0010 时代41/50 959/959 [==============================]-0s 66us / step-损耗:5.9270-精度:0.0010 时代42/50 959/959 [==============================]-0s 68us / step-损耗:5.9081-精度:0.0010 时代43/50 959/959 [==============================]-0s 68us / step-损耗:5.8894-精度:0.0010 时代44/50 959/959 [==============================]-0s 68us / step-损耗:5.8709-精度:0.0010 时代45/50 959/959 [==============================]-0s 68us / step-损耗:5.8526-精度:0.0010 时代46/50 959/959 [==============================]-0s 67us / step-损耗:5.8346-精度:0.0010 时代47/50 959/959 [==============================]-0s 64us / step-损耗:5.8168-精度:0.0010 时代48/50 959/959 [=============================]-0s 70us / step-损耗:5.7992-精度:0.0010 时代49/50 959/959 [==============================]-0s 63us / step-损耗:5.7818-精度:0.0010 时代50/50 959/959 [==============================]-0s 64us / step-损耗:5.7646-精度:0.0010 耗时:3.9185秒。 240/240 [==============================]-0s 154us / step 测试损失:595.13%,准确度0.00% 花费时间:3.9664秒
答案 0 :(得分:0)
您说您只有数字数据(1199行,每个行有43个特征),没有图像。但是在模型中,您使用2D卷积,通常用于图像。您还可以将第一层的input_shape指定为839x43。我明白了,因为839的底数是1199 * 0.7。但这意味着您的模型将看到您输入的是一个单一图像,而不是每个具有43个特征的839个不同实例。您可能想使用1D卷积或仅使用密集层来完成任务。