相同的代码,在windows / ubuntu(Keras / Tensorflow)上的准确度非常不同

时间:2017-10-10 17:46:55

标签: windows ubuntu tensorflow keras

import pandas as pd
import numpy as np
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import LSTM
from keras.optimizers import Adam
from sklearn.preprocessing import MinMaxScaler

def create_dataset(dataset, datasetClass, look_back):
    dataX, dataY = [], []
    for i in range(len(dataset)-look_back-1):
        a = dataset[i:(i+look_back), 0]
        dataX.append(a)
        dataY.append(datasetClass[:,(i+look_back):(i+look_back+1)])

    return np.array(dataX), np.array(dataY)

def one_hot_encode(dataset):
    data = np.zeros((11, len(dataset)),dtype='int')
    for i in range(len(dataset)):
        data[dataset[i]-1,i] = 1
    return data

#Set a seed for repeatable results
np.random.seed(12)


dataframe = pd.read_csv('time-series.csv', usecols=[1], engine='python')
dataset = dataframe.values
dataset = dataset.astype('float32')

dataframeClass = pd.read_csv('time-series-as-class.csv', usecols=[1], engine='python')
datasetClass = dataframeClass.values
datasetClass = datasetClass.astype('int')

datasetClass = one_hot_encode(datasetClass)

#normalize input vals
scaler = MinMaxScaler(feature_range=(0, 1))
dataset = scaler.fit_transform(dataset)


#separate to test/train
train_size = int(len(dataset) * 0.67)
test_size = len(dataset) - train_size
train, test = dataset[0:train_size, :], dataset[train_size:len(dataset), :]
trainClass, testClass = datasetClass[:, 0:train_size,], datasetClass[:, train_size:len(dataset)]

#set up sliding windows
look_back = 150
trainX, trainY = create_dataset(train, trainClass, look_back)
testX, testY = create_dataset(test, testClass, look_back)


#reformat for proper passing to nn
trainX = np.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1]))
testX = np.reshape(testX, (testX.shape[0], 1, testX.shape[1]))
trainY = np.squeeze(trainY, 2)
testY = np.squeeze(testY, 2)

# create and fit the LSTM network
model = Sequential()
model.add(LSTM(15, input_shape=(1,look_back)))
model.add(Dense(22,activation='tanh'))
model.add(Dropout(0.2))
model.add(Dense(11,activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer=Adam(), metrics=['categorical_accuracy'])
print(model.summary())
model.fit(trainX, trainY, epochs=90, batch_size=1, verbose=2)
# make predictions
trainPredict = model.predict(trainX)
testPredict = model.predict(testX)

我在Ubuntu和Windows上运行它。使用keras v 2.0.4和2.0.8在Windows上测试,在ubuntu上使用2.0.5(最新版本通过conda提供)

窗口的准确度为17%,分类交叉熵为2,它慢慢收敛但始终从那里开始

ubuntu的准确率为98%,分类交叉熵显示为0,实际上并没有改变

唯一的代码区别是csv文件的路径,csv文件完全相同。什么可能导致如此巨大的差异?

如果差异是一两个百分点,我可以把它写下来作为dropout / tf随机初始化,但是因为它是太多而不是纯粹的机会

编辑:解决方案被证明是修复了分类csv文件,虽然它们是utf-8显然还有其它东西需要让它们在windows中创建时与linux一起玩。我不确定我是否可以将自己的答案标记为“已接受”

2 个答案:

答案 0 :(得分:3)

问题证明是在csv文件中,这些文件最初是从Windows移植的。虽然它们以utf-8格式保存,但我还是需要去libreoffice并将它们保存为linux csv文件。

在初始状态下,它们没有加载失败,但没有进行适当的单热编码,导致所有单热编码为0.显然,这会导致非常高的精度。

答案 1 :(得分:1)

导入keras之前需要设置

np.random.seed(12)