我原来的keras.Tensorflow 2.0由GPU运行
但我尝试运行此代码
for label in range(10):
print('***** label {} *****'.format(label))
y_c = model.output[0, label]
grads = keras.backend.gradients(y_c, model.input)[0]
saliency = keras.backend.function([model.input], [grads])
idx_coord = np.where(y_test == label)[0]
X = np.zeros((batch_size, 28, 28, 1))
for batch in range(batch_size):
X[batch,...,0] = x_test[idx_coord[batch]]/255
Y = np.array([[1 if label==i else 0 for i in range(10)] for _ in range(batch_size)])
for batch in tqdm.trange(batch_size):
x_input = X[batch,...,0].copy()
y_input = Y[batch].copy()
score_list = []
if isplot:
plt.figure(figsize=(30,30))
for it in range(iteration):
score = model.evaluate(x_input[np.newaxis,...,np.newaxis], y_input[np.newaxis,...], verbose=0)[1]
score_list.append(score)
#img, gradmap =get_gradcam(x_test[idx], model, idx,"conv2d_33")
gradmap = np.abs(np.squeeze(get_smoothgrad(x_input[np.newaxis,...,np.newaxis], saliency, sigma=sigma)))
if isplot:
plt.subplot(1,20,2*(it%10)+1)
plt.imshow(np.squeeze(x_input))
plt.subplot(1,20,2*(it%10)+2)
plt.imshow(np.squeeze(gradmap))
if it%10 == 9:
plt.show()
plt.figure(figsize=(30,30))
coord = np.where(gradmap == gradmap.max())
x_input[coord] = 0.
tot_score[label].append(score_list)
我不确定mnist的批处理方式如何工作
但是gpu没有运行此代码
- 该批次与培训批次有什么不同?
- 为什么此代码不是由gpu运行的?