问题
I'm playing with some 2D CNN using Keras to predict Bike Sharing Demand.
R performs very poorly vs Python, which reach good accuracy easily. I thought it was because of arrays shape (and some differences between R and Python), so I play with that for a while, ultimately using all possible shapes.
I created the CombinationGrid
object elsewhere and it looks like this:
+------+------+------+------+-------+
| Dim1 | Dim2 | Dim3 | Dim4 | Order |
+------+------+------+------+-------+
| 8887 | 3 | 2 | 1 | F |
| 3 | 8887 | 2 | 1 | F |
| 8887 | 2 | 3 | 1 | C |
| 2 | 8887 | 3 | 1 | C |
+------+------+------+------+-------+
It is a table with combinations for 4th dimensional arrays (is used in the code, where it will be more clear). And here's the full version of that, just for reproducibility
Here's the R Code:
#Read data
TrainDF=read_delim(file='train.csv', delim=',')
#Subset
X_Train=TrainDF[2000:nrow(TrainDF),c('temp', 'atemp', 'humidity', 'windspeed', 'casual', 'registered')]
Y_Train=as.matrix(TrainDF[2000:nrow(TrainDF),c('count')])
#YVal
YVal=as.matrix(Y_Train)
#For loop and try all combinations
Results=list()
for(i in 1:nrow(CombinationGrid)){
#Reshape using all possible combinations
XVal=array_reshape(x=as.matrix(X_Train), dim=CombinationGrid[i,1:4], order=CombinationGrid[i,]$Order)
#Keras Model
model=keras_model_sequential()
model %>%
layer_conv_2d(filters=10, kernel_size=c(2,2), padding='same', activation='relu') %>%
layer_conv_2d(filters=15, kernel_size=c(2,2), padding='same', activation='relu') %>%
layer_conv_2d(filters=20, kernel_size=c(3,3), padding='same') %>%
layer_max_pooling_2d(pool_size=c(2,2), strides=1) %>%
layer_flatten() %>%
layer_dense(units=30, activation='relu') %>%
layer_dense(units=20, activation='relu') %>%
layer_dense(units=10, activation='relu') %>%
layer_dense(units=1)
#Compile model
model %>% compile(
loss = 'mse',
optimizer = optimizer_adam(),
metrics = c('accuracy'))
#Train model
Hist=tryCatch({
model %>% fit(XVal, YVal, epochs = 100)
},error=function(e){
Hist=list('metrics'=list('loss'=NA, 'acc'=NA))
})
#Save results
Results[[i]]=list('Loss'=Hist$metrics$loss[length(Hist$metrics$loss)], 'Acc'=Hist$metrics$acc[length(Hist$metrics$acc)])
}
Here's the Python code:
#Read Combination Gird
CombinationGrid=pd.read_table('CombinationGrid.txt')
#Read Dataset
TrainDF = pd.read_csv('train.csv', parse_dates=["datetime"])
#Subset training data
X_Train= TrainDF[1999:]
#Create responser variable
YVal = X_Train[['count']]
#Turn into numpy array
YVal=np.array(YVal)
#Select only usefull parameters
X_Train = X_Train[['temp', 'atemp', 'humidity', 'windspeed', 'casual', 'registered']]
#For loop to try all combinations
Results=[]
for i in range(0,CombinationGrid.shape[0]):
XVal = np.array(X_Train, dtype=np.float32).reshape(tuple(CombinationGrid.iloc[i,])[0:4], order=tuple(CombinationGrid.iloc[i,])[4])
model=keras.Sequential()
model.add(keras.layers.Conv2D(filters=10, kernel_size=[2,2], padding='same', activation='relu'))
model.add(keras.layers.Conv2D(filters=15, kernel_size=[2,2], padding='same', activation='relu'))
model.add(keras.layers.Conv2D(filters=20, kernel_size=[3,3], padding='same'))
model.add(keras.layers.MaxPooling2D(pool_size=[2,2], strides=1))
model.add(keras.layers.Flatten())
model.add(keras.layers.Dense(units=30, activation='relu'))
model.add(keras.layers.Dense(units=20, activation='relu'))
model.add(keras.layers.Dense(units=10, activation='relu'))
model.add(keras.layers.Dense(units=1))
model.compile(optimizer='adam', loss='mse', metrics=['accuracy'])
#Save results
try:
Hist=model.fit(XVal, YVal, epochs=100)
Results.append((Hist.history['loss'][len(Hist.history['loss'])-1],Hist.history['accuracy'][len(Hist.history['accuracy'])-1]))
except:
Results.append((np.nan, np.nan))
pass
Results:
I saved both R and Python results and here they are. All the other array shapes for the data failed in both Python and R (probably because of Y's not having suitable shape to match predictors):
+------+------+------+------+-------+-------------+-------------+-------------+-------------+
| Dim1 | Dim2 | Dim3 | Dim4 | Order | R Loss | R Acc | Python Loss | Python Acc |
+------+------+------+------+-------+-------------+-------------+-------------+-------------+
| 8887 | 3 | 2 | 1 | F | 0.257986314 | 0.004726004 | 0.264519099 | 0.86125803 |
| 8887 | 2 | 3 | 1 | F | 1.922012638 | 0.004726004 | 0.375910975 | 0.780578375 |
| 8887 | 3 | 2 | 1 | C | 0.062438282 | 0.004726004 | 4.27717965 | 0.700686395 |
| 8887 | 2 | 3 | 1 | C | 0.171041382 | 0.004726004 | 0.054061489 | 0.95262742 |
+------+------+------+------+-------+-------------+-------------+-------------+-------------+
As you can see, the last Losses look similar, but the last recorded Accuracy is hugely different between both. I know I have some fault regarding dimension and shape understandment in both R and Python and how they differ, but after trying every possible shape and getting no similar result, it turns weird. Also, Keras Accuracy in R seems to never change!
I couldn't find more info on the matter, only another post stating the contrary situation.
So, something is happening, it may be my fault but I don't know why, if I use the same data, can't get a good score using Keras in R as I do in Python. Any ideas?
回答1:
Well, as Skeydan explained to me in the issue I opened, the difference in accuracy falls in the Keras version used.
In the Python code, changing from import keras
to import tensorflow.keras as keras
makes the accuracy to match between both R and Python.
I found more information about this here and here.
来源:https://stackoverflow.com/questions/59462354/discrepancy-between-rs-keras-and-pythons-keras-accuracy-bug