使用AddN构建tensorflow简单图例

该文定义了一个基于Tensorflow的线性模型,使用了多个占位符(placeholder)来输入数据,计算图的输出是所有输入的和。使用numpy创建测试数组并展示了它们的加法操作。虽然文中提到了训练过程,但未给出完整的训练代码。

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

import tensorflow.compat.v1 as tf
import numpy as np
### Define a model: a computational graph
# Parameters for a linear model y = Wx + b

# Placeholder for input and prediction
x1 = tf.placeholder(dtype=tf.float32, shape=[1, 5])
x2 = tf.placeholder(dtype=tf.float32, shape=[1, 2, 5])
x3 = tf.placeholder(dtype=tf.float32, shape=[None,5])
x4 = tf.placeholder(dtype=tf.float32, shape=[None,5])
x5 = tf.placeholder(dtype=tf.float32, shape=[None,5])
x6 = tf.placeholder(dtype=tf.float32, shape=[None,5])
x7 = tf.placeholder(dtype=tf.float32, shape=[None,5])
x8 = tf.placeholder(dtype=tf.float32, shape=[None,5])


x = [x1, x2, x3, x4, x5, x6, x7, x8]
# Define output = x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8
output = tf.math.add_n(x)
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    summary_writer = tf.summary.FileWriter('./', sess.graph)

a = np.array([[0, 0, 0],
              [1, 1, 1],
              [2, 2, 2],
              [3, 3, 3]])
b = np.array([1, 2, 3])
print(a.shape)
print(b.shape)
print(a + b)

#
# with tf.Session() as sess:
#     # Retrieve the variable initializer op and initialize variable W & b.
#     sess.run(tf.global_variables_initializer())
#     for i in range(1000):
#         sess.run(train, {x:x_train, y:y_train})
#         if i%100==0:
#             l_cost = sess.run(loss, {x:x_train, y:y_train})
#             print(f"i: {i} cost: {l_cost}")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

C__Try

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值