0
回答
Tensorflow使用cpu计算与使用gpu结果不同
终于搞明白,存储TCO原来是这样算的>>>   

最近在学习TensorFlow,在一个给定数据集的情况下,写了一个keras的程序,分别在cpu和gpu上运行时,出现了一个问题:

    使用cpu时,训练过程中损失函数loss为nan, 准确率acc为0;

    使用gpu时,训练过程中损失函数loss为正常数据,准确率也是正常的。

GPU运算结果:

GPU运算结果

CPU运算结果(第一轮训练时时loss不为nan):

不明白为何会出现这样的结果,GPU不是只影响其速度吗?请大能解惑……

代码如下,使用的版本1.13GPU版本,处理器是Core i5,GPU GeForce 840M。

数据格式:训练数据维度49, 标签1位(0或1)。

# coding: utf-8

# In[1]:

import arff
import time
import keras
import numpy as np
import tensorflow as tf
from keras import regularizers
from sklearn import preprocessing
from sklearn.utils import shuffle
from keras.utils import np_utils
from keras.models import Sequential
from keras.optimizers import SGD, Adam, RMSprop
import keras.backend.tensorflow_backend as KTF
from keras.layers.core import Dense, Dropout, Activation, Flatten

#KTF.set_session(tf.Session(config=tf.ConfigProto(device_count={'cpu':0})))

# data load
file_ = open('CBZL.arff', 'rb')
decoder = arff.ArffDecoder()
dataset = decoder.decode(file_, encode_nominal=True)

data    = np.array(dataset['data'])
x0      = data[:,0:-1]
y0      = data[:,-1]
print(data.shape)

# model train
N_batch    = 32
N_epoch    = 100
per_train  = 0.8
N_nodes    = 100



model = Sequential()
model.add(Dense(N_nodes, input_dim=x0.shape[1]))
model.add(Activation('relu'))
model.add(Dropout(0.2))
model.add(Dense(N_nodes))
model.add(Activation('relu'))
model.add(Dropout(0.2))
model.add(Dense(N_nodes))
model.add(Activation('relu'))
model.add(Dropout(0.2))
model.add(Dense(1))
model.add(Activation('sigmoid'))
model.compile(loss='binary_crossentropy',
              optimizer='rmsprop', metrics=['accuracy'])

# data preprocessing 
x1, y1  = shuffle(x0, y0)
X_train = x1[0:int(x0.shape[0] * per_train)]
X_test  = x1[int(x0.shape[0] * per_train)::]
Y_train = y1[0:int(x0.shape[0] * per_train)]
Y_test  = y1[int(x0.shape[0] * per_train)::]
print X_train.shape
print X_train.dtype


start = time.time()
history = model.fit(X_train, Y_train,
                        batch_size=N_batch, epochs=N_epoch,
                        verbose=1)
end = time.time()
print 'time elapse training:\t', end - start, 'sec'

# model evaluation
start = time.time()  
score_train = model.evaluate(X_train, Y_train, batch_size=N_batch, verbose=0)
end = time.time()
print 'time elapse:\t', end - start, 'sec'
print 'Train accuracy:', score_train[1]
start = time.time()  
score_test = model.evaluate(X_test, Y_test, batch_size=N_batch, verbose=0)
end = time.time()
print 'time elapse:\t', end - start, 'sec'
print 'Test accuracy:', score_test[1]

 

<无标签>
举报
Booobi
发帖于2周前 0回/22阅
顶部