Keras on TensorFlow in GCP

by allenlu2007

先說結論:如何在 GCP 上 install keras 同時可以 visualize 結果。


Method I:  Anaconda + tensorflow + keras (on VNC):  tf 是 virtue machine

(a) 最簡單的方式是 install anaconda -> tensorflow (見前文)

(b) 在來只要 > conda install keras 就搞定。

另外如果需要 display, 建議用 VNC 搭配。同樣見前文。


VNC client display 結果如下。python code 見下文。

(Server:  run > vncserver)  if need restart vncserver kill :1 then rerun vncserver)

NewImage


Method II:

(a) Google cloud shell + 8081 web preview + tensorflow (default) + Jupyter notebook (default). (見前文)

(b) 唯一的問題是如何 install keras in Jupyter notebook  => 在 jupyter notebook 用

! pip install keras  (Bingo!)

=> 後台好像變成 theano.

=> 不過執行好像自動切回 tensorflow!

 

Keras_autoencoder.ipynb

!pip install keras
Collecting keras
  Downloading Keras-2.0.6.tar.gz (228kB)
    100% |################################| 235kB 2.9MB/s
Collecting theano (from keras)
  Downloading Theano-0.9.0.tar.gz (3.1MB)
    100% |################################| 3.1MB 309kB/s
Requirement already satisfied: pyyaml in /usr/local/lib/python2.7/dist-packages (from keras)
Requirement already satisfied: six in /usr/local/lib/python2.7/dist-packages (from keras)
Requirement already satisfied: numpy>=1.9.1 in /usr/local/lib/python2.7/dist-packages (from theano->keras)
Requirement already satisfied: scipy>=0.14 in /usr/local/lib/python2.7/dist-packages (from theano->keras)
Building wheels for collected packages: keras, theano
  Running setup.py bdist_wheel for keras ... -   \   done
  Stored in directory: /root/.cache/pip/wheels/c2/80/ba/2beab8c2131e2dcc391ee8a2f55e648af66348115c245e0839
  Running setup.py bdist_wheel for theano ... -   \   |   /   done
  Stored in directory: /root/.cache/pip/wheels/d5/5b/93/433299b86e3e9b25f0f600e4e4ebf18e38eb7534ea518eba13
Successfully built keras theano Installing collected packages: theano, keras Successfully installed keras-2.0.6 theano-0.9.0
from keras.layers import Input, Dense
from keras.models import Model

# this is the size of our encoded representations
encoding_dim = 32  # 32 floats -> compression of factor 24.5, assuming the input is 784 floats

# this is our input placeholder
input_img = Input(shape=(784,))
# "encoded" is the encoded representation of the input
encoded = Dense(encoding_dim, activation='relu')(input_img)
# "decoded" is the lossy reconstruction of the input
decoded = Dense(784, activation='sigmoid')(encoded)

# this model maps an input to its reconstruction
autoencoder = Model(input_img, decoded)
Using TensorFlow backend.
# this model maps an input to its encoded representation
encoder = Model(input_img, encoded)
# create a placeholder for an encoded (32-dimensional) input
encoded_input = Input(shape=(encoding_dim,))
# retrieve the last layer of the autoencoder model
decoder_layer = autoencoder.layers[-1]
# create the decoder model
decoder = Model(encoded_input, decoder_layer(encoded_input))
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')
from keras.datasets import mnist
import numpy as np
(x_train, _), (x_test, _) = mnist.load_data()
Downloading data from https://s3.amazonaws.com/img-datasets/mnist.npz
11075584/11490434 [===========================>..] - ETA: 0s
x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.
x_train = x_train.reshape((len(x_train), np.prod(x_train.shape[1:])))
x_test = x_test.reshape((len(x_test), np.prod(x_test.shape[1:])))
print(x_train.shape)
print(x_test.shape)
(60000, 784)
(10000, 784)
autoencoder.fit(x_train, x_train,
                epochs=50,
                batch_size=256,
                shuffle=True,
                validation_data=(x_test, x_test))
Train on 60000 samples, validate on 10000 samples
Epoch 1/50
60000/60000 [==============================] - 8s - loss: 0.3702 - val_loss: 0.2734
Epoch 2/50
60000/60000 [==============================] - 8s - loss: 0.2670 - val_loss: 0.2574
Epoch 3/50
60000/60000 [==============================] - 8s - loss: 0.2475 - val_loss: 0.2353
Epoch 4/50
60000/60000 [==============================] - 8s - loss: 0.2265 - val_loss: 0.2159
Epoch 5/50
60000/60000 [==============================] - 8s - loss: 0.2100 - val_loss: 0.2020
Epoch 6/50
60000/60000 [==============================] - 8s - loss: 0.1980 - val_loss: 0.1916
….
Epoch 48/50
60000/60000 [==============================] - 8s - loss: 0.1045 - val_loss: 0.1026
Epoch 49/50
60000/60000 [==============================] - 8s - loss: 0.1041 - val_loss: 0.1023
Epoch 50/50
60000/60000 [==============================] - 8s - loss: 0.1037 - val_loss: 0.1020
<keras.callbacks.History at 0x7fa430902dd0>
# encode and decode some digits
# note that we take them from the *test* set
encoded_imgs = encoder.predict(x_test)
decoded_imgs = decoder.predict(encoded_imgs)
# use Matplotlib (don't ask)
import matplotlib.pyplot as plt

n = 10  # how many digits we will display
plt.figure(figsize=(20, 4))
for i in range(n):
    # display original
    ax = plt.subplot(2, n, i + 1)
    plt.imshow(x_test[i].reshape(28, 28))
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)

    # display reconstruction
    ax = plt.subplot(2, n, i + 1 + n)
    plt.imshow(decoded_imgs[i].reshape(28, 28))
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)
plt.show()

 NewImage


Advertisements