tensorflow 仿真已知简单神经网络

这里写图片描述
图 1-1 待用tf仿真的已知网络结构

"""
用tf仿真一个已知的简单神经网络,
网络的图片地址:    https://img-blog.csdn.net/20170816081420914
预期的结果使用ndarray计算, 与tf的输出结果作对比
"""
import tensorflow as tf
import numpy as np

x_mat = [[1, 2]]
w1_mat = np.array([[0, 1], [2, 3]])
w2_mat = np.array([[1], [2]])

x = tf.constant(x_mat)
w1 = tf.Variable(w1_mat)
w2 = tf.Variable(w2_mat)
a = tf.matmul(x, w1)
y = tf.matmul(a, w2)


def calc_tf():
    with tf.Session() as sess:
        sess.run(w1.initializer)
        sess.run(w2.initializer)
        result = sess.run(y)
    return result


def calc_ndarray():
    tmp = np.dot(x_mat, w1_mat)
    tmp = np.dot(tmp, w2_mat)
    return tmp


if (calc_tf() == calc_ndarray()):
    print('They\'re equal, and value is ' + (str)(calc_tf()))
else:
    print('something wrong')

"""
They're equal, and value is [[18]]
"""
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值