问题
Why Tensorflow results are different in Python versions 3.5 (SQL server, machine learning services) and 3.7 (local machine, anaconda)?
I found out, it depends on 4 parameters values:
dataset size
number of epochs
number of 1st layer (input) neurons
number of 2nd layer (hidden) neurons
Here is the example: identical results:
dataset size - 50 000
number of epochs - 5/3/2
number of 1st layer (input) neurons - 300
**number of 2nd layer (hidden) neurons - 80% from 1st layer**
different results:
dataset size - 50 000
number of epochs - 6/5/3/2
number of 1st layers neurons - 300
**number of 2nd layer (hidden) neurons - 40% from 1st layer**
Seems that the problem is in the 4th parameter, but:
dataset size - 50 000
**number of epochs - 6!**
number of 1st layer (input) neurons - 300
number of 2nd layer (hidden) neurons - 80% from 1st layer
Gives different results. By decreasing the other 3 parameters values it is possible to achieve identical results, but it is uncomfortable.
Package versions are identical on both the platforms:
2.2.0 tensorflow
0.24.2 pandas
2.3.1 keras
1.17.5 numpy
Datasets are also absolutely identical.
The code:
#general libraries
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
#neural network
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from keras.optimizers import adam
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import confusion_matrix
np.random.seed(7)
import tensorflow as tf
tf.random.set_seed(7)
import random
random.seed(7)
layer1neurons = 300
layer2neurons_share = .4
loss="binary_crossentropy"
learning_rate = 0.001
optimizer=tf.keras.optimizers.Adam(lr=learning_rate)
metrics=["accuracy"]
epochs = 6
batch_size = 32
verbose = 1
model = Sequential()
model.add(Dense(layer1neurons, input_dim=len(X_train.columns), activation="relu"))
model.add(Dense(round(layer1neurons * layer2neurons_share, 0), activation="relu"))
model.add(Dense(1, activation="sigmoid"))
model.compile(loss=loss,
optimizer=optimizer,
metrics=metrics)
model.fit(X_train, y_train,
epochs = epochs,
batch_size=batch_size,
verbose = verbose)
scores = model.evaluate(X_train, y_train)
来源:https://stackoverflow.com/questions/62128556/why-tensorflow-results-are-different-in-python-versions-3-5-and-3-7