zoukankan      html  css  js  c++  java
  • tensorflow学习笔记1

    tensorflow处理的是图形图像,图形图形都是二维平面,所以数据都用矩阵来表示。

    定义w,x,注意定义方式,进行矩阵相乘matmul()函数时,w,x必须是一行一列的表示方式,否则会报错。

    import tensorflow as tf
    
    #定义w,x,y
    w = tf.Variable([[0.5,1.0]]) #一行 分别是0.5 1.0
    x = tf.Variable([[2.0],[1.0]]) #两行
    y = tf.matmul(w,x)
    
    #定义一种操作是把全部变量初始化
    init_op = tf.compat.v1.global_variables_initializer()
    #这些操作必须在Session里面跑
    with tf.compat.v1.Session() as sess:
        sess.run(init_op)
        #输出y的值要调用eval()函数输出
        print(y.eval())

     另一种写法:先用placeholder进行占位,再在Session()中喂数据

    #另一种写法
    input1 = tf.compat.v1.placeholder(tf.float32,shape=[2,1])
    input2 = tf.compat.v1.placeholder(tf.float32,shape=[1,2])
    output = tf.matmul(input1,input2)
    
    with tf.compat.v1.Session() as sess:
        sess.run([output],feed_dict={input1:[[0.5,1.0]],input2:[[2.0],[1.0]]})
        print(output.eval())

    注:该段代码有误。

    遇到的问题:喂入的数据是二维的时候,placeholder的shape出现了问题,我认为我写的是正确的,但会报错,还没找到错误原因,如有大佬请指正!

    Traceback (most recent call last):
      File "F:/python/learn/03.py", line 23, in <module>
        sess.run([output],feed_dict={input1:[[0.5,1.0]],input2:[[2.0],[1.0]]})
      File "D:Anancondalibsite-packages ensorflowpythonclientsession.py", line 950, in run
        run_metadata_ptr)
      File "D:Anancondalibsite-packages ensorflowpythonclientsession.py", line 1149, in _run
        str(subfeed_t.get_shape())))
    ValueError: Cannot feed value of shape (1, 2) for Tensor 'Placeholder:0', which has shape '(2, 1)'

  • 相关阅读:
    面向对象
    ArrayList 集合
    JAVA 方法
    JAVA数组
    JAVA基础2
    JAVA基础1(语法)
    JAVA基础(jdk安装和环境变量的配置)
    数据结构练习题
    多表查询
    数据约束
  • 原文地址:https://www.cnblogs.com/xrj-/p/14258725.html
Copyright © 2011-2022 走看看