经过一些迭代后,损失突然变成了楠

时间:2018-02-24 20:04:20

标签: python tensorflow machine-learning deep-learning

## Read the datasheet

X, Y = read_dataset()

model_path = "/Users/shalinsavalia/Desktop/ECG_CNN/CNN"

 ## Shuffle the dataset to mix up the rows

X, Y = shuffle(X, Y, random_state=1)

 ## Convert the dataset into train and test part

train_x, test_x, train_y, test_y = train_test_split(X, Y, test_size=0.20, random_state=415)

print "Shape of ECG signals in training dataset {}".format(train_x.shape)

print "Shape of ECG diseases in training dataset {}".format(train_y.shape)

print "Shape of ECG signals in testing dataset {}".format(test_x.shape)

print "Shape of ECG diseases in testing dataset {}".format(test_y.shape)

 ## Define the important parameters and variables to work with tensors

n_dim = X.shape[1]

n_class = 9

 # Input - shape 'None' states that, the value can be anything, i.e we can feed in any number of ECG signals

 ## Input ECG signals length 

x=tf.placeholder(tf.float32,shape=[None,n_dim])

 ## Input class ( having total 9 Dieseases )

y_=tf.placeholder(tf.float32,shape=[None, n_class])

 ## Reshape the Input signal for Feeding to CNN

x_input=tf.reshape(x,[-1,1,60,1], name='input')

 ## First convolutional layer with 32 output filters, filter size 5x5, stride of 2,same padding, and RELU activation.

conv_layer1=tflearn.layers.conv.conv_2d(x_input, nb_filter=32, filter_size=5, strides=[1,1,1,1],
                                        padding='same', activation='relu', regularizer="L2", name='conv_layer_1')

out_layer1=tflearn.layers.conv.max_pool_2d(conv_layer1, 2)


 ## Second Convolutional layer

conv_layer2=tflearn.layers.conv.conv_2d(out_layer1, nb_filter=128, filter_size=5, strides=[1,1,1,1],
                                        padding='same', activation='relu',  regularizer="L2", name='conv_layer_2')

out_layer2=tflearn.layers.conv.max_pool_2d(conv_layer2, 2)

 ## Third Convolutional layer 

conv_layer3=tflearn.layers.conv.conv_2d(out_layer2, nb_filter=64, filter_size=5, strides=[1,1,1,1],
                                        padding='same', activation='relu',  regularizer="L2", name='conv_layer_3')

out_layer3=tflearn.layers.conv.max_pool_2d(conv_layer3, 2)

 ## Fourth Convolutional layer 

conv_layer4=tflearn.layers.conv.conv_2d(out_layer3, nb_filter=32, filter_size=5, strides=[1,1,1,1],
                                        padding='same', activation='relu',  regularizer="L2", name='conv_layer_4')

out_layer4=tflearn.layers.conv.max_pool_2d(conv_layer4, 2)

 ## Fully connected layer

fcl= tflearn.layers.core.fully_connected(out_layer4, 1024, activation='relu')

fcl_dropout = tflearn.layers.core.dropout(fcl, 0.8)

y_predicted = tflearn.layers.core.fully_connected(fcl_dropout, 9, activation='softmax', name='output')

 ## Loss function

cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y_predicted), reduction_indices=[1]))

 ## Optimiser 

train_step = tf.train.GradientDescentOptimizer(0.2).minimize(cross_entropy)

 ## Calculating accuracy of our model 

correct_prediction = tf.equal(tf.argmax(y_predicted,1), tf.argmax(y_,1))

accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
  

结果看起来像这个

Shape of ECG diseases in testing dataset (5309, 9)

Shape of input : [None, 1, 60, 1]

Shape of first convolutional layer : [None, 1, 30, 32]

Shape of second convolutional layer : [None, 1, 15, 128]

Shape of third convolutional layer : [None, 1, 15, 64]

Shape of fourth convolutional layer : [None, 1, 15, 32]

Shape of fully connected layer : [None, 1024]

Shape of output layer : [None, 9]

('Epoch :', 198, '- Loss: ', 1.046878, '- Train Accuracy: ',
0.6050674)

('Epoch :', 199, '- Loss: ', 1.0282028, '- Train Accuracy: ',
0.60676277)

('Epoch :', 200, '- Loss: ', 1.0455937, '- Train Accuracy: ',
0.59508336)

('Epoch :', 201, '- Loss: ', 1.0259817, '- Train Accuracy: ',
0.6227277)

('Epoch :', 202, '- Loss: ', 1.0068184, '- Train Accuracy: ',
0.61241406)

('Epoch :', 203, '- Loss: ', 0.9958509, '- Train Accuracy: ',
0.6385985)

('Epoch :', 204, '- Loss: ', nan, '- Train Accuracy: ', 0.6148159)

('Epoch :', 205, '- Loss: ', nan, '- Train Accuracy: ', 0.118065365)

('Epoch :', 206, '- Loss: ', nan, '- Train Accuracy: ', 0.118065365)

('Epoch :', 207, '- Loss: ', nan, '- Train Accuracy: ', 0.118065365)

('Epoch :', 208, '- Loss: ', nan, '- Train Accuracy: ', 0.118065365)

如果你看到我的训练,最初的损失一直在下降到203个纪元,突然之间它降到了纳米,我试图改变一些卷积层,最大的池层但是,它仍然不起作用。在所有情况下,它在一些时代之后变成了纳米。在所有情况下我都观察到一件事,当损失达到.98或0.99左右后,它就变成了纳米。我不知道我的代码有什么问题吗?请帮助我,我真的很感激。

1 个答案:

答案 0 :(得分:2)

以下交叉修改代码不安全

cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y_predicted), reduction_indices=[1]))

因为当y_predicted将零传递给它时,log具有未定义的值。

解决这个问题请使用此

cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(tf.clip_by_value(y_predicted,1e-10,1.0)), reduction_indices=[1]))
相关问题