gpt4 book ai didi

python - 在多种选项中,如何在 TensorFlow 中随机执行其中一种?

转载 作者:太空宇宙 更新时间:2023-11-03 21:02:45 24 4
gpt4 key购买 nike

如何以可训练的方式从多个备选方案中随机选择一个执行流程?例如:

import random
from tensorflow import keras

class RandomModel(keras.Model):
def __init__(self, model_set):
super(RandomModel, self).__init__()
self.models = model_set


def call(self, inputs):
"""Calls one of its models at random"""
return random.sample(self.models, 1)[0](inputs)


def new_model():
return keras.Sequential([
keras.layers.Dense(10, activation='softmax')
])

model = RandomModel({new_model(), new_model()})
model.build(input_shape=(32, 784))
model.summary()

虽然此代码 runs ,它似乎不允许梯度反向传播。这是它的输出:

_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
Total params: 0
Trainable params: 0
Non-trainable params: 0
_________________________________________________________________

最佳答案

我找到了a way去做这个。但是,由于嵌套的 tf.cond 操作,执行速度很慢:

def random_network_applied_to_inputs(inputs, networks):
"""
Returns a tf.cond tree that does binary search
before applying a network to the inputs.
"""
length = len(networks)

index = tf.random.uniform(
shape=[],
minval=0,
maxval=length,
dtype=tf.dtypes.int32
)

def branch(lower_bound, upper_bound):
if lower_bound + 1 == upper_bound:
return networks[lower_bound](inputs)
else:
center = (lower_bound + upper_bound) // 2
return tf.cond(
pred=index < center,
true_fn=lambda: branch(lower_bound, center),
false_fn=lambda: branch(center, upper_bound)
)

return branch(0, length)

关于python - 在多种选项中,如何在 TensorFlow 中随机执行其中一种?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55618901/

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