问题描述
我只是深度学习领域的新手.
I am just a novice in area of deep learning.
我第一次使用Keras Conv1D做基本尝试.不知道我做了什么以及我是否做对了.我输入的数据只是美国各地商店每周(总计313周)的总销售额,时间步长为1.
I made my first basic attempt with Keras Conv1D. Not sure what I did and whether I did it right. My input data is simply total sales by every week (total of 313 weeks), for stores across US and with a time step of 1.
这是我的代码:
from pandas import read_csv
import matplotlib.pyplot as plt
import numpy
from keras.datasets import imdb
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Flatten
from keras.layers.convolutional import Conv1D
from keras.layers.convolutional import MaxPooling1D
from keras.layers.embeddings import Embedding
from keras.preprocessing import sequence
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error
def create_dataset(dataset, look_back=1):
dataX, dataY = [], []
for i in range(len(dataset)-look_back):
a = dataset[i:(i+look_back), 0]
dataX.append(a)
dataY.append(dataset[i + look_back, 0])
return numpy.array(dataX), numpy.array(dataY)
seed = 7
numpy.random.seed(seed)
dataframe = read_csv('D:/MIS793/Dataset/Academic Dataset External 2/Python scripts/totalsale _byweek.csv', usecols=[1], engine='python')
plt.plot(dataframe)
plt.show()
dataset = dataframe.values
dataset = dataset.astype('float32')
# normalize the dataset
scaler = MinMaxScaler(feature_range=(0, 1))
dataset = scaler.fit_transform(dataset)
train_size = int(len(dataset) * 0.67)
test_size = len(dataset) - train_size
train, test = dataset[0:train_size,:], dataset[train_size:len(dataset),:]
# reshape into X=t and Y=t+1
look_back = 1
trainX, trainY = create_dataset(train, look_back)
testX, testY = create_dataset(test, look_back)
trainX = trainX.reshape(trainX.shape[0], trainX.shape[1], 1).astype('float32')
testX = testX.reshape(testX.shape[0], testX.shape[1], 1).astype('float32')
model = Sequential()
model.add(Conv1D(filters=10, kernel_size=1, padding='same', strides=1, activation='relu',input_shape=(1,1)))
model.add(MaxPooling1D(pool_size=1))
model.add(Flatten())
model.add(Dense(250, activation='relu'))
model.add(Dense(1, activation='linear'))
model.compile(loss='mse', optimizer='adam', metrics=['mae'])
print(model.summary())
model.fit(trainX, trainY, validation_data=(testX, testY), epochs=10, batch_size=100)
scores = model.evaluate(testX, testY, verbose=0)
print("Accuracy: %.2f%%" % (scores[1]*100))
不确定这里的几件事:
- 重新定义trainX和testX.
- kernel_size和input_shape的值
我的想法是,这只是销售价值的一个载体. 10个过滤器,每个大小为1,从一个值移到另一个值.输入形状的格式为时间步长,尺寸.
My idea here is it's just one vector of sales value. 10 filters, each of size 1 move from one value to another. Input shape is of the format time step, dimensions.
我的准确率只有10.91%!所以我的第一个问题是我是否输入了正确的参数.
I only got accuracy of 10.91%! So my first question is whether I am feeding in the right parameters.
谢谢
ASC
推荐答案
使用 model.metrics_names ,您可以获取得分变量的标签.
With model.metrics_names you can get the labels of your scores variable.
在您的情况下为 ['loss','mean_absolute_error'] .
所以您要打印的不是准确性,而是 mae 乘以100.
So what you are printing is not the accuracy, but the mae, multiplied by 100.
这篇关于时间序列的Keras Conv1D的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!