keras博客自动编码器代码我正在尝试从
来运行卷积自动码的代码https://blog.keras.io/building-autoencoders-in-keras.html
from keras.layers import Input, Dense, Convolution2D, MaxPooling2D, UpSampling2D
from keras.models import Model
input_img = Input(shape=(1, 28, 28))
x = Convolution2D(16, 3, 3, activation='relu', border_mode='same')(input_img)
x = MaxPooling2D((2, 2), border_mode='same')(x)
x = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(x)
x = MaxPooling2D((2, 2), border_mode='same')(x)
x = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(x)
encoded = MaxPooling2D((2, 2), border_mode='same')(x)
# at this point the representation is (8, 4, 4) i.e. 128-dimensional
Convolution2D(8, 3, 3, activation='relu', border_mode='same')(encoded)
x = UpSampling2D((2, 2))(x)
x = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(x)
x = UpSampling2D((2, 2))(x)
x = Convolution2D(16, 3, 3, activation='relu')(x)
x = UpSampling2D((2, 2))(x)
decoded = Convolution2D(1, 3, 3, activation='sigmoid', border_mode='same')(x)
autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')
运行后,我运行此代码进行培训:
from keras.datasets import mnist
import numpy as np
(x_train, _), (x_test, _) = mnist.load_data()
x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.
x_train = np.reshape(x_train, (len(x_train), 1, 28, 28))
x_test = np.reshape(x_test, (len(x_test), 1, 28, 28))
现在我想绘制我使用回调的结果!我键入此
tensorboard --logdir=/tmp/autoencoder
在我的终端中,它成功切换回Theano,但是当我运行
时from keras.callbacks import TensorBoard
autoencoder.fit(x_train, x_train,
nb_epoch=50,
batch_size=128,
shuffle=True,
validation_data=(x_test, x_test),
callbacks=[TensorBoard(log_dir='/tmp/autoencoder')])
它仍然意味着不切换回张量。有人知道如何解决吗?
RuntimeError Traceback (most recent call last)
<ipython-input-4-fc8458b2c2ba> in <module>()
6 shuffle=True,
7 validation_data=(x_test, x_test),
----> 8 callbacks=[TensorBoard(log_dir='/tmp/autoencoder')])
/home/hoda/anaconda2/lib/python2.7/site-packages/keras/callbacks.pyc in __init__(self, log_dir, histogram_freq, write_graph, write_images)
487 super(TensorBoard, self).__init__()
488 if K._BACKEND != 'tensorflow':
--> 489 raise RuntimeError('TensorBoard callback only works '
490 'with the TensorFlow backend.')
491 self.log_dir = log_dir
RuntimeError: TensorBoard callback only works with the TensorFlow backend.
要切换到TensorFlow后端,您必须编辑位于~/.keras
中的keras.json
文件。
您应该看到一条"backend": "theano"
,将" Theano"更改为" TensorFlow",如果正确安装了TensorFlow,则使用TensorFlow Backend使用" TensorFlow"。导入keras时应出现。