tensorflow keras 实现xor 异或的网络实现 Python3代码

代码托管在github

https://github.com/sofiathefirst/AIcode

 

程序运行环境和版本

a@ubuntu:~/Desktop/mytf$ python3
Python 3.5.2 (default, Nov 12 2018, 13:43:14) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as  tf
>>> tf.__version__
'1.13.1'
>>> tf.keras.__version__
'2.2.4-tf'
>>> 


结果

tensorflow keras 实现xor 异或的网络实现 Python3代码_第1张图片

python3 代码

 

#!/usr/bin/env python3   
# -*- coding:UTF-8 -*-  
import tensorflow as tf
import numpy as np
from tensorflow.python import debug as tf_debug

#tf.keras.backend.set_session(tf_debug.LocalCLIDebugWrapperSession(tf.Session()))
import scipy.misc

x_train = np.array([[1,1],[0,0],[0,1],[1,0]]);
y_train = np.array([0,0,1,1])
model = tf.keras.models.Sequential([
  tf.keras.layers.Dense(2, activation='sigmoid'),

  tf.keras.layers.Dense(1, activation='sigmoid')
])
model.compile(optimizer='adam',
              loss='mean_squared_error',
              metrics=['binary_accuracy'])

model.fit(x_train, y_train, epochs=7000)
model.save('/home/a/my_minis.h5')
new_model = tf.keras.models.load_model('/home/a/my_minis.h5')
print(new_model.predict(x_train))
print(new_model.predict_classes(x_train))

 

你可能感兴趣的:(keras,xor,tensorflow,python3,python,深度学习,tensorflow)