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

本文介绍了一个使用TensorFlow 1.13.1和Keras 2.2.4-tf版本进行简单的人工智能代码训练的例子。该示例通过构建一个包含两层的神经网络模型来解决异或(XOR)问题,并展示了如何保存和加载训练好的模型。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码托管在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'
>>> 


结果

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))

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值