《智能计算系统》书2.11作业
题目:设计一个多层感知器实现4位加法器的功能,即两个4比特输入得到一个4比特输出和一个1比特进位。请自行构造训练集和测试集,完成训练及测试。
我使用的是TensorFlow 2,需要关闭Eager Execution 实现TensorFlow 1.x 版本中的静态图形编程模型。
# 导入 TensorFlow 库
import tensorflow as tf
# 关闭 Eager Execution 模式
tf.compat.v1.disable_eager_execution()
# 定义输入和输出
inputs = tf.compat.v1.placeholder(tf.float32, shape=[None, 8])
outputs = tf.compat.v1.placeholder(tf.float32, shape=[None, 5])
# 创建一个全连接层和输出层,定义损失函数,使用梯度下降优化损失函数。
# 创建一个全连接层
hidden_layer = tf.keras.layers.Dense(8, activation='relu')(inputs)
# 创建一个输出层
#predictions = tf.keras.layers.Dense(4, activation='sigmoid')(hidden_layer)
predictions = tf.keras.layers.Dense(5, activation='sigmoid')(hidden_layer)
#