【问题标题】:Tensorflow - use previously learned weights to initialize new weights of different dimensionsTensorflow - 使用先前学习的权重来初始化不同维度的新权重
【发布时间】:2018-02-17 18:46:32
【问题描述】:

我正在尝试使用先前学习的维度m 的权重来初始化维度n 的权重张量,其中n > m。我可以像下面那样做。

all_weights['w1'] = tf.Variable(tf.zeros([n, output_sz], dtype=tf.float32))
all_weights['w1'] = all_weights['w1'][:m,:].assign(initial_weights['w1'])

但是,我稍后在实际学习发生时遇到了一个问题,如果我不使用权重共享,我就不会遇到。 w1 最初是一个 tf.Variable,我注意到它在切片分配后变为张量对象:Tensor("strided_slice/_assign:0")。我的问题是我收到错误:

`LookupError: No gradient defined for operation 'strided_slice_2/_assign' (op type: StridedSliceAssign)`.

这是否与类型有关(Tensor vs tf.Variable)?对于某些人来说,将张量转换为 tf.Variable 是否有意义?我尝试这样做,但随后出现如下错误:

`FailedPreconditionError: Attempting to use uninitialized value Variable_4
 [[Node: strided_slice/_assign = StridedSliceAssign[Index=DT_INT32, T=DT_FLOAT, _class=["loc:@Variable_4"], begin_mask=3, ellipsis_mask=0, end_mask=2, new_axis_mask=0, shrink_axis_mask=0, _device="/job:localhost/replica:0/task:0/cpu:0"](Variable_4, strided_slice/stack, strided_slice/stack_1, strided_slice/stack_2, strided_slice/_assign/value)]]`

我对 Tensorflow 比较陌生,因此我们将不胜感激。谢谢!

【问题讨论】:

    标签: python tensorflow


    【解决方案1】:

    tf.VariableTensor 完全不同。在它们之间“投射”是没有意义的。

    最简单的解决方案是直接在Variable 创建中使用initial_weights。例如,像这样:

    import numpy as np
    tf.Variable(np.append(initial_weights['w1'],
                          np.zeros((n-m, output_sz)),
                          axis=0),
                dtype=tf.float32)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-15
      • 2020-10-27
      • 1970-01-01
      • 1970-01-01
      • 2017-03-12
      • 2017-03-14
      • 2019-10-09
      • 2018-09-05
      相关资源
      最近更新 更多