gpt4 book ai didi

python - 如何在自定义层中编写初始化程序(random.uniform)?

转载 作者:行者123 更新时间:2023-12-01 01:04:35 29 4
gpt4 key购买 nike

我想用随机制服初始化我的自定义图层。在TensorFlow中,我可以找到以下使用initializer='uniform'的代码。但我想在 (-1.0,1.0) 之间设置随机统一输出范围。如何做到这一点:

class MyDenseLayer(tf.keras.layers.Layer):
def __init__(self, num_outputs):
super(MyDenseLayer, self).__init__()
self.num_outputs = num_outputs

def build(self, input_shape):
self.kernel = self.add_variable(initializer='uniform',shape=[int(input_shape[-1]),self.num_outputs])

def call(self, input):
return tf.matmul(input, self.kernel)

最佳答案

一种方法是在 numpy 中生成随机统一,然后使用 tf.constant_initializer(),如下所示:

import tensorflow as tf
import numpy as np

class MyDenseLayer(tf.keras.layers.Layer):
def __init__(self, num_outputs):
super(MyDenseLayer, self).__init__()
self.num_outputs = num_outputs

def build(self, input_shape):
shape = [int(input_shape[-1]),self.num_outputs]
init_val = np.random.uniform(low=-1.0, high=1.0, size=shape)
initializer = tf.constant_initializer(init_val,
dtype=tf.float32)
self.kernel = self.add_weight(initializer=initializer,
shape=shape,
name='kernel')
super(MyDenseLayer, self).build(input_shape)

def call(self, input):
return tf.matmul(input, self.kernel)

改用tf.initializers.random_uniform()

init = tf.initializers.random_uniform(minval=-1.0, maxval=1.0)
self.kernel = self.add_weight(initializer=init,
shape=shape,
name='kernel')

关于python - 如何在自定义层中编写初始化程序(random.uniform)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55495085/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com