[PYTHON] I tried to integrate with Keras in TFv1.1

TensorFlow v1.1 Release, which was a bit annoying because there was no mention of XLA, but as the roadmap shows, Keras It seems that the integration with is starting. It's still like RC, but it's one of the highlights personally, and I was afraid to try it, so I tried it.

I used the Keras code I wrote earlier in the article entitled Miscellaneous commentary on TensorFlow's High Level API.

Sample code

import tensorflow as tf
from tensorflow.contrib.keras.python import keras
from sklearn import cross_validation

#Data preparation
iris = tf.contrib.learn.datasets.base.load_iris()
train_x, test_x, train_y, test_y = cross_validation.train_test_split(
    iris.data, iris.target, test_size=0.2
)

num_classes = 3
train_y = keras.utils.to_categorical(train_y, num_classes)
test_y = keras.utils.to_categorical(test_y, num_classes)

#Model definition
model = Sequential()

#Network definition
model.add(Dense(10, activation='relu', input_shape=(4,)))
model.add(Dense(20, activation='relu'))
model.add(Dense(10, activation='relu'))
model.add(Dense(3, activation='softmax'))

#Confirmation of model summary
model.summary()

#Compiling the model
model.compile(loss='categorical_crossentropy',
              optimizer='sgd',
              metrics=['accuracy'])

#Learning
history = model.fit(train_x, train_y,
                    batch_size=100,
                    epochs=2000,
                    verbose=1,
                    validation_data=(test_x, test_y))

#Evaluation of learning model
score = model.evaluate(x_test, y_test, verbose=0)

print('Test loss:', score[0])
print('Test accuracy:', score[1])

result

--As you've heard, you can access Keras modules by just ʻimport`` tensorflow.contrib.keras.python, so it looks like a good idea. ――By combining with Estimator, I could see the future where the division of labor between data scientists and engineers would progress. ――I have a feeling that CloudML Engine` will be easier to use.

I've only tried it quickly, so I'd like to discuss it in detail again.

Recommended Posts

I tried to integrate with Keras in TFv1.1
I tried to move GAN (mnist) with keras
I tried to implement Grad-CAM with keras and tensorflow
I tried to create an article in Wiki.js with SQLAlchemy
I tried to implement PLSA in Python
I tried to implement Autoencoder with TensorFlow
I tried to implement permutation in Python
I tried to visualize AutoEncoder with TensorFlow
I tried to implement PLSA in Python 2
I tried to implement ADALINE in Python
I tried to implement PPO in Python
I tried to implement CVAE with PyTorch
I tried to solve TSP with QAOA
I tried to describe the traffic in real time with WebSocket
I tried to make deep learning scalable with Spark × Keras × Docker
I tried to process the image in "sketch style" with OpenCV
I tried to process the image in "pencil style" with OpenCV
I tried to log in to twitter automatically with selenium (RPA, scraping)
I tried to predict next year with AI
I tried to detect Mario with pytorch + yolov3
I tried to implement reading Dataset with PyTorch
I tried to use lightGBM, xgboost with Boruta
I tried to learn logical operations with TF Learn
I tried to detect motion quickly with OpenCV
I tried to get CloudWatch data with Python
I tried to output LLVM IR with Python
I tried to debug.
I tried to implement TOPIC MODEL in Python
I tried to detect an object with M2Det!
I tried to automate sushi making with python
I tried to predict Titanic survival with PyCaret
I tried to paste
I tried to operate Linux with Discord Bot
I tried to implement selection sort in python
I tried to study DP with Fibonacci sequence
I tried to start Jupyter with Amazon lightsail
I tried to judge Tsundere with Naive Bayes
I implemented the VGG16 model in Keras and tried to identify CIFAR10
I tried to train the RWA (Recurrent Weighted Average) model in Keras
I tried to implement merge sort in Python with as few lines as possible
I tried to predict the horses that will be in the top 3 with LightGBM
I tried to make deep learning scalable with Spark × Keras × Docker 2 Multi-host edition
I tried to learn the sin function with chainer
I tried to move machine learning (ObjectDetection) with TouchDesigner
I tried to graph the packages installed in Python
I tried to create a table only with Django
I tried to extract features with SIFT of OpenCV
I tried to move Faster R-CNN quickly with pytorch
I tried to read and save automatically with VOICEROID2 2
I want to transition with a button in flask
I tried to implement and learn DCGAN with PyTorch
I tried to implement Minesweeper on terminal with python
I tried to get started with blender python script_Part 01
I tried to draw a route map with Python
I tried to solve the soma cube with python
I tried to implement a pseudo pachislot in Python
I tried to automatically read and save with VOICEROID2
I tried to get started with blender python script_Part 02
I tried to implement Dragon Quest poker in Python
I tried to generate ObjectId (primary key) with pymongo
I was addicted to scraping with Selenium (+ Python) in 2020