我只是参加了我的第一个深度学习项目,以熟悉keras和tf。 这是一个多标签分类问题,我使用一个简单的卷积网络从XRD模式中识别出各个化合物。
我花了一些时间,但结果确实让我震惊。我没想到我的简单网络在准确性方面会表现得那么好。
然后我意识到我一直在使用theano作为后端。 因此,我再次尝试使用tensorflow,并且相同的输入数据给出了可怕的结果。甚至比随机森林分类器还差。
我几乎一无所知。 我想我做错了,但我只是想不通....
无论如何,这是代码(输入数据在下面提供...)
Theano_vs_TF.py:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
import os
os.environ['KERAS_BACKEND']='theano'
from keras.models import Model
from keras.models import Sequential
import keras as K
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
X=np.loadtxt('X.txt')
Y=np.loadtxt('Y.txt')
X3d = np.expand_dims(X, axis=-1)
# start with TF-model
tf_model = keras.Sequential()
wreg = tf.keras.regularizers.l2(l=0) # dont use for now
tf_model.add(layers.Conv1D(32, 8,strides=8, padding='same',
input_shape=(180,1), activation='relu',kernel_regularizer=wreg))
tf_model.add(layers.Conv1D(32, 5,strides=5, padding='same',
activation='relu',kernel_regularizer=wreg))
tf_model.add(layers.Conv1D(16, 3,strides=3, padding='same',
activation='relu',kernel_regularizer=wreg))
tf_model.add(layers.Flatten())
tf_model.add(layers.Dense(512,activation='relu',kernel_regularizer=wreg))
tf_model.add(layers.Dense(29, activation='sigmoid',kernel_regularizer=wreg))
tf_optimizer = tf.keras.optimizers.Adam(learning_rate=1e-3)
tf_model.compile(loss='binary_crossentropy',
optimizer=tf_optimizer,
metrics=['accuracy'],
)
tf_model.summary()
# train tf-model
tf_history = tf_model.fit(X3d,Y,batch_size=128,
epochs=50, verbose=1,validation_split=0.3,
)
plt.plot(tf_history.history['accuracy'])
plt.plot(tf_history.history['val_accuracy'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'val'], loc='upper left')
plt.show()
# now compare to theano model
th_model = Sequential() #old way
wreg = K.regularizers.l2(l=0.0) # 1e-3 was way too much, no hints for overfitting so far
th_model.add(K.layers.Conv1D(32, 8,strides=8, padding='same',
input_shape=(180,1), activation='relu',
kernel_regularizer=wreg))
th_model.add(K.layers.Conv1D(32, 5,strides=5,
padding='same', activation='relu',kernel_regularizer=wreg))
th_model.add(K.layers.Conv1D(16, 3,strides=3, padding='same', activation='relu',kernel_regularizer=wreg))
th_model.add(K.layers.Flatten())
th_model.add(K.layers.Dense(512,activation='relu',kernel_regularizer=wreg))
th_model.add(K.layers.Dense(29, activation='sigmoid'))
#Define optimizer
optimizer = K.optimizers.adam(lr=1e-3)
# Compile model
th_model.compile(loss='binary_crossentropy',
optimizer=optimizer,
metrics=['accuracy'],
)
th_model.summary()
# train th model and plot metrics
th_history = th_model.fit(X3d,Y,batch_size=128,
epochs=50, verbose=1,validation_split=0.3,
)
plt.plot(th_history.history['acc'])
plt.plot(th_history.history['val_acc'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'val'], loc='upper left')
plt.show()