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

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',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],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一起玩.我不确定我是否可以将自己的答案标记为“已接受”

解决方法

事实证明这个问题出现在csv文件中,这些文件最初是从windows中移植过来的.虽然它们以utf-8格式保存,我仍然需要去libreoffice并将它们保存为linux csv文件.

在他们的初始状态,他们没有加载失败,但没有正确的单热编码,导致所有单热编码为0.显然,这导致非常高的准确性.

相关文章

Windows2012R2备用域控搭建 前置操作 域控主域控的主dns:自...
主域控角色迁移和夺取(转载) 转载自:http://yupeizhi.blo...
Windows2012R2 NTP时间同步 Windows2012R2里没有了internet时...
Windows注册表操作基础代码 Windows下对注册表进行操作使用的...
黑客常用WinAPI函数整理之前的博客写了很多关于Windows编程的...
一个简单的Windows Socket可复用框架说起网络编程,无非是建...