tf.placeholder_with_default案例

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Jun  7 11:17:10 2019

@author: muli
"""

import tensorflow as tf
import numpy as np

def mand(a):
    # tf.placeholder_with_default
    #a = tf.Variable(1,shape=[1,3])
    x = tf.placeholder_with_default(a, shape=(None, 3))
    b=tf.constant(2, shape=[3, 2],dtype=tf.float32)
    print(b.eval())
    print("b-type:",type(b))
    print("***************")
    res = tf.matmul(x, b)
    print("x-type:",type(x))
    print("res-type:",type(res))
    print("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@2")
    return x,res

with tf.Session() as sess:
#    a=tf.constant(5, shape=[1, 3])
    a = tf.Variable([[5,5,5]],dtype=tf.float32)
    print(a)
    print("a-type:",type(a))
    print("***************")
    s = np.random.rand(2, 3)
    print(s)
    print("s-type:",type(s))
    print("------------------------")
    # ding-yi
    x1,res1=mand(a)
    init = tf.global_variables_initializer()
    sess.run(init)

    print(res1.eval())
    print("res1:",np.shape(res1))
    print("res1:",np.shape(res1.eval()))
    print("************************")
    
    print("s-shape:",np.shape(s))
    print("s-type:",type(s))
    print("x1-shape:",np.shape(x1))
    print("x1-type:",type(x1))
    print()
    a1,b1=sess.run([x1,res1], feed_dict={x1: s}) # Will succeed.
#    print(sess.run([x1,res1], feed_dict={x1: s}))
    print("a1:",a1)
    print("a1-type:",type(a1))
    print()
    print("b1:",b1)
    print("b1-type:",type(b1))
    print()
上一篇:新手必备 | PyTorch基础入门教程(一)


下一篇:Pytorch:Tensor和Autograd