tensorflow 仿真已知简单神经网络

来源:互联网 发布:如何查看手机端口命令 编辑:程序博客网 时间:2024/06/01 09:35

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

"""用tf仿真一个已知的简单神经网络,网络的图片地址:    http://img.blog.csdn.net/20170816081420914预期的结果使用ndarray计算, 与tf的输出结果作对比"""import tensorflow as tfimport numpy as npx_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 resultdef calc_ndarray():    tmp = np.dot(x_mat, w1_mat)    tmp = np.dot(tmp, w2_mat)    return tmpif (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]]"""
原创粉丝点击