python人工智能tensorflow函数tf.assign使用方法

目录
  • 参数数量及其作用
  • 例子

参数数量及其作用

该函数共有五个参数,分别是:

  • 被赋值的变量 ref
  • 要分配给变量的值 value、
  • 是否验证形状 validate_shape
  • 是否进行锁定保护 use_locking
  • 名称 name
def assign(ref, value, validate_shape=None, use_locking=None, name=None)
Update 'ref' by assigning 'value' to it.
This operation outputs a Tensor that holds the new value of 'ref' after
the value has been assigned. This makes it easier to chain operations
that need to use the reset value.
Args:
  ref: A mutable `Tensor`.
	Should be from a `Variable` node. May be uninitialized.
  value: A `Tensor`. Must have the same type as `ref`.
    The value to be assigned to the variable.
  validate_shape: An optional `bool`. Defaults to `True`.
    If true, the operation will validate that the shape
    of 'value' matches the shape of the Tensor being assigned to.  If false,
    'ref' will take on the shape of 'value'.
  use_locking: An optional `bool`. Defaults to `True`.
    If True, the assignment will be protected by a lock;
    otherwise the behavior is undefined, but may exhibit less contention.
  name: A name for the operation (optional).
Returns:
  A `Tensor` that will hold the new value of 'ref' after
  the assignment has completed.

该函数的作用是将一个要分配给变量的值value赋予被赋值的变量ref,用于tensorflow各个参数的变量赋值。

例子

该例子将举例如何进行变量之间的数据赋值和如何进行集合间的数据赋值。

import tensorflow as tf;
import numpy as np;
c1 = ['c1', tf.GraphKeys.GLOBAL_VARIABLES]
c2 = ['c2', tf.GraphKeys.GLOBAL_VARIABLES]
#常量初始化器
v1_cons = tf.get_variable('v1_cons',dtype = tf.float32,shape=[1,4], initializer=tf.constant_initializer(), collections = c1)
v2_cons = tf.get_variable('v2_cons',dtype = tf.float32,shape=[1,4], initializer=tf.constant_initializer(9), collections = c1)
#正太分布初始化器
v1_nor = tf.get_variable('v1_nor',dtype = tf.float32, shape=[1,4], initializer=tf.random_normal_initializer(mean=0, stddev=5), collections = c2)
v2_nor = tf.get_variable('v2_nor',dtype = tf.float32, shape=[1,4], initializer=tf.random_normal_initializer(mean=0, stddev=5), collections = c2)
assign1 = tf.assign(v1_cons,v2_cons)    #将v2_cons赋予v1_cons
c1_get = tf.get_collection('c1')        #获得c1集合
c2_get = tf.get_collection('c2')        #获得c2集合
assign2 = [tf.assign(cg1,cg2) for cg1,cg2 in zip(c1_get,c2_get) ]   #将c2赋予c1
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print("v1_cons:",sess.run(v1_cons))
    print("v2_cons:",sess.run(v2_cons))
    print(sess.run(assign1))            #显示赋值后的结果
    print("将v2_cons赋予v1_cons:",sess.run(v1_cons))
    print("c1_get_collection:",sess.run(c1_get))
    print("c2_get_collection:",sess.run(c2_get))
    print(sess.run(assign2))            #显示赋值后的结果
    print("将c2赋予c1:",sess.run(c1_get))

其输出为:

v1_cons: [[0. 0. 0. 0.]]
v2_cons: [[9. 9. 9. 9.]]
[[9. 9. 9. 9.]]
将v2_cons赋予v1_cons: [[9. 9. 9. 9.]]
c1_get_collection: [array([[9., 9., 9., 9.]], dtype=float32), array([[9., 9., 9., 9.]], dtype=float32)]
c2_get_collection: [array([[-3.9746916, -7.5332146,  2.4480317, -1.3282107]], dtype=float32), array([[10.687443 ,  3.6653206,  1.7079141, -4.524155 ]], dtype=float32)]
[array([[-3.9746916, -7.5332146,  2.4480317, -1.3282107]], dtype=float32), array([[10.687443 ,  3.6653206,  1.7079141, -4.524155 ]], dtype=float32)]
将c2赋予c1: [array([[-3.9746916, -7.5332146,  2.4480317, -1.3282107]], dtype=float32), array([[10.687443 ,  3.6653206,  1.7079141, -4.524155 ]], dtype=float32)]

以上就是python人工智能tensorflow函数tf.assign使用方法的详细内容,更多关于tensorflow函数tf.assign的资料请关注我们其它相关文章!

(0)

相关推荐

  • python人工智能tensorflow常用激活函数Activation Functions

    目录 常见的激活函数种类及其图像 1 sigmoid(logsig)函数 2 tanh函数 3 relu函数 4 softplus函数 tensorflow中损失函数的表达 1 sigmoid(logsig)函数 2 tanh函数 3 relu函数 4 softplus函数 激活函数在机器学习中常常用在神经网络隐含层节点与神经网络的输出层节点上,激活函数的作用是赋予神经网络更多的非线性因素,如果不用激励函数,输出都是输入的线性组合,这种情况与最原始的感知机相当,网络的逼近能力相当有限.如果能够引

  • python人工智能tensorflowtf优化器Optimizer算法汇总

    目录 前言 tensorflow常见的Optimizer 1 梯度下降法 2 Adagrad下降法 3 动量优化法 4 RMSProp算法 5 Adam算法 例子 1 梯度下降法 2 Adagrad下降法 3 动量优化法 4 RMSProp算法 5 Adam算法 总结 前言 优化器的选择关乎参数更新的方法,合理的方法可以帮助机器学习更好的寻找到全局最佳值.那我们快点开始学习吧 tensorflow常见的Optimizer 1 梯度下降法 tf.train.GradientDescentOptim

  • python人工智能tensorflow函数tf.nn.dropout使用方法

    目录 前言 tf.nn.dropout函数介绍 例子 代码 keep_prob = 0.5 keep_prob = 1 前言 神经网络在设置的神经网络足够复杂的情况下,可以无限逼近一段非线性连续函数,但是如果神经网络设置的足够复杂,将会导致过拟合(overfitting)的出现,就好像下图这样. 看到这个蓝色曲线,我就知道: 很明显蓝色曲线是overfitting的结果,尽管它很好的拟合了每一个点的位置,但是曲线是歪歪曲曲扭扭捏捏的,这个的曲线不具有良好的鲁棒性,在实际工程实验中,我们更希望得到

  • python人工智能tensorflow函数tensorboard使用方法

    目录 tensorboard相关函数及其常用参数设置 1 with tf.name_scope(layer_name): 2 tf.summary.histogram(layer_name+"/biases",biases) 3 tf.summary.scalar(“loss”,loss) 4 tf.summary.merge_all() 5 tf.summary.FileWriter(“logs/”,sess.graph) 6 write.add_summary(result,i)

  • python人工智能tensorflow函数tf.get_variable使用方法

    目录 参数数量及其作用 例子 参数数量及其作用 该函数共有十一个参数,常用的有: 名称name 变量规格shape 变量类型dtype 变量初始化方式initializer 所属于的集合collections def get_variable(name, shape=None, dtype=None, initializer=None, regularizer=None, trainable=True, collections=None, caching_device=None, partiti

  • python人工智能tensorflow函数tf.layers.dense使用方法

    目录 参数数量及其作用 部分参数解释: 示例 参数数量及其作用 tf.layers.dense用于添加一个全连接层. 函数如下: tf.layers.dense( inputs, #层的输入 units, #该层的输出维度 activation=None, #激活函数 use_bias=True, kernel_initializer=None, # 卷积核的初始化器 bias_initializer=tf.zeros_initializer(), # 偏置项的初始化器 kernel_regul

  • python人工智能tensorflow函数tf.assign使用方法

    目录 参数数量及其作用 例子 参数数量及其作用 该函数共有五个参数,分别是: 被赋值的变量 ref 要分配给变量的值 value. 是否验证形状 validate_shape 是否进行锁定保护 use_locking 名称 name def assign(ref, value, validate_shape=None, use_locking=None, name=None) Update 'ref' by assigning 'value' to it. This operation outp

  • python人工智能tensorflow函数tf.get_collection使用方法

    目录 参数数量及其作用 例子 参数数量及其作用 该函数共有两个参数,分别是key和scope. def get_collection(key, scope=None) Wrapper for Graph.get_collection() using the default graph. See tf.Graph.get_collection for more details. Args: key: The key for the collection. For example, the `Gra

  • python人工智能tensorflow函数np.random模块使用方法

    目录 np.random模块常用的一些方法介绍 例子 numpy.random.rand(d0, d1, …, dn): numpy.random.randn(d0, d1, …, dn): numpy.random.randint(low, high=None, size=None, dtype=‘I’): numpy.random.uniform(low=0.0, high=1.0, size=None): numpy.random.normal(loc=0.0, scale=1.0, si

  • python人工智能TensorFlow自定义层及模型保存

    目录 一.自定义层和网络 1.自定义层 2.自定义网络 二.模型的保存和加载 1.保存参数 2.保存整个模型 一.自定义层和网络 1.自定义层 ①必须继承自layers.layer ②必须实现两个方法,__init__和call 这个层,实现的就是创建参数,以及一层的前向传播. 添加参数使用self.add_weight,直接调用即可,因为已经在母类中实现. 在call方法中,实现前向传播并返回结果即可. 2.自定义网络 ①必须继承自keras.Model ②必须实现两个方法,__init__和

  • python人工智能tensorflow构建循环神经网络RNN

    目录 学习前言 RNN简介 tensorflow中RNN的相关函数 tf.nn.rnn_cell.BasicLSTMCell tf.nn.dynamic_rnn 全部代码 学习前言 在前一段时间已经完成了卷积神经网络的复习,现在要对循环神经网络的结构进行更深层次的明确. RNN简介 RNN 是当前发展非常火热的神经网络中的一种,它擅长对序列数据进行处理. 什么是序列数据呢?举个例子. 现在假设有四个字,“我” “去” “吃” “饭”.我们可以对它们进行任意的排列组合. “我去吃饭”,表示的就是我

随机推荐