我正在尝试在keras中使用tensorboard。以下是我的代码:
from keras.layers import merge, Dropout, Convolution2D, MaxPooling2D, Input, Dense, Flatten, Merge
from keras.models import Model
from keras.callbacks import EarlyStopping, ReduceLROnPlateau,TensorBoard
import pickle
from sklearn.utils import shuffle
import numpy as np
import random
from keras.optimizers import Adam
import tensorflow as tf
import keras.backend.tensorflow_backend as KTF
np.random.seed(1000)
def load_pickled_data(file, columns):
with open(file, mode='rb') as f:
dataset = pickle.load(f)
return tuple(map(lambda c: dataset[c], columns))
train_preprocessed_dataset_file = "train.p"
test_preprocessed_dataset_file = "test.p"
X_train, y_train_64 = load_pickled_data(train_preprocessed_dataset_file, columns = ['features', 'labels'])
X_test, y_test_64 = load_pickled_data(test_preprocessed_dataset_file, columns = ['features', 'labels'])
y_train = y_train_64.astype(np.float32)
y_test = y_test_64.astype(np.float32)
old_session = KTF.get_session()
with tf.Graph().as_default():
session = tf.Session('')
KTF.set_session(session)
KTF.set_learning_phase(1)
###CNN model###
input_img = Input(shape=(32, 32, 1))
conv_1 = Convolution2D(32, 5, 5, border_mode='same', activation='relu')(input_img)
pool_1 = MaxPooling2D((2, 2))(conv_1)
pool_1 = Dropout(0.1)(pool_1)
conv_2 = Convolution2D(64, 5, 5, border_mode='same', activation='relu')(pool_1)
pool_2 = MaxPooling2D((2, 2))(conv_2)
pool_2 = Dropout(0.2)(pool_2)
conv_3 = Convolution2D(128, 5, 5, border_mode='same', activation='relu')(pool_2)
pool_3 = MaxPooling2D((2, 2))(conv_3)
pool_3 = Dropout(0.3)(pool_3)
pool_3 = Flatten()(pool_3)
pool_1 = MaxPooling2D((4, 4))(pool_1)
pool_1 = Flatten()(pool_1)
pool_2 = MaxPooling2D((2, 2))(pool_2)
pool_2 =Flatten()(pool_2)
all_features = merge([pool_1, pool_2, pool_3], mode='concat')
logits = Dense(500,activation='relu')(all_features)
logits = Dropout(0.5)(logits)
res = Dense(43,activation='softmax')(logits)
c_model = Model(input_img, res)
c_model.summary()
adam = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08)
c_model.compile(loss='categorical_crossentropy', optimizer= adam, metrics=['accuracy'])
tensor_board = TensorBoard(log_dir='./logs', histogram_freq=1)
history = c_model.fit(X_train, y_train, batch_size=128,nb_epoch=3,shuffle=True,verbose=1,validation_split=0.25,callbacks=[tensor_board])
loss_and_metrics = c_model.evaluate(X_test, y_test, batch_size=128)
KTF.set_session(old_session)
但是错误发生如下:
文件 “/home/jasontian/enter/lib/python3.5/site-packages/spyder/utils/site/sitecustomize.py” 第866行,在runfile execfile(文件名,命名空间)
中文件 “/home/jasontian/enter/lib/python3.5/site-packages/spyder/utils/site/sitecustomize.py” 第102行,在execfile exec中(compile(f.read(),filename,'exec'), 命名空间)
文件“/media/jasontian/keras_tf.py”,第111行,在history =中 c_model.fit(X_train,y_train, 的batch_size = 128,nb_epoch = 3,洗牌=真,冗长= 1,validation_split = 0.25,回调= [tensor_board])
文件 “/home/jasontian/enter/lib/python3.5/site-packages/keras/engine/training.py” 第1196行,in fit initial_epoch = initial_epoch)文件 “/home/jasontian/enter/lib/python3.5/site-packages/keras/engine/training.py” 第911行,在_fit_loop callbacks.on_epoch_end(epoch,epoch_logs)
文件 “/home/jasontian/enter/lib/python3.5/site-packages/keras/callbacks.py” 第76行,在on_epoch_end callback.on_epoch_end(纪元,日志)
文件 “/home/jasontian/enter/lib/python3.5/site-packages/keras/callbacks.py” 第653行,在on_epoch_end结果= self.sess.run([self.merged], feed_dict = feed_dict)
文件 “/home/jasontian/enter/lib/python3.5/site-packages/tensorflow/python/client/session.py” 第766行,运行run_metadata_ptr)
文件 “/home/jasontian/enter/lib/python3.5/site-packages/tensorflow/python/client/session.py” 第921行,在_run + e.args [0])TypeError:无法将feed_dict键解释为Tensor:无法将int转换为Tensor。
起初我以为它可能是y_train.dtype
(它是float64),但我发现它在一个例子中效果很好。
更新:X_train的形状是(39209,32,32,1)。
那我怎么解决这个问题呢?
答案 0 :(得分:1)
没有tf会话会不会有效?如果你真的不需要会话,你可以试试这个:
awk '/\home\/user\//{print $2}' file.txt
如果您的keras默认后端是Tensorflow,则不必指定它。