gpt4 book ai didi

python - 名称错误 : name 'GATE_OP' is not defined #tensorflow

转载 作者:行者123 更新时间:2023-12-01 09:21:11 27 4
gpt4 key购买 nike

我使用 Tensorflow 的时间不长,遇到了一个我不太理解的问题。这是导致问题的代码,因为 PyCharm 不知道 GATE_OP:

class GradientDescentOptimizer(Optimizer, tf.train.GradientDescentOptimizer):
def compute_gradients(self, loss, var_list=None,
gate_gradients=GATE_OP,
aggregation_method=None,
colocate_gradients_with_ops=False,
grad_loss=None):
if var_list is not None:
for v in var_list:
v = v + epsilon
if var_list is None:
var_list = utils.hyperparameters()
for v in var_list:
v = v + epsilon
grads_and_vars = super(GradientOracle, self).compute_gradients(loss, var_list)
return grads_and_vars

def apply_gradients(self, grads_and_vars, global_step=None, name=None):
ts = super(GradientOracle, self).apply_gradients(grads_and_vars, global_step, name)
dynamics = OrderedDict()
for g, w in grads_and_vars:
wk = w - tf.cast(self._learning_rate_tensor, g.dtype) * g
dynamics[w] = wk
return ts, dynamics

def __str__(self):
return '{}_lr{}'.format(self._name, self._learning_rate)

@property
def learning_rate(self):
return self._learning_rate

@property
def learning_rate_tensor(self):
return self._learning_rate_tensor

当我尝试执行示例时,我总是收到 NameError: name 'GATE_OP' is not Defined。但我导入了 Tensorflow。

这是我从 git 存储库重用的示例 https://github.com/lucfra/FAR-HO/blob/master/far_ho/examples/Example_weighted_error(and_lr_and_w0).ipynb :

from __future__ import absolute_import, print_function, division

import far_ho as far
import tensorflow as tf
import far_ho.examples as far_ex

import os
import matplotlib.pyplot as plt
import seaborn as sbn

sbn.set_style('whitegrid')
#%matplotlib inline

tf.reset_default_graph()
ss = tf.InteractiveSession()

x = tf.placeholder(tf.float32, shape=(None, 28**2), name='x')
y = tf.placeholder(tf.float32, shape=(None, 10), name='y')

# load a small portion of mnist data
datasets = far_ex.mnist(folder=os.path.join(os.getcwd(), 'MNIST_DATA'), partitions=(.1, .1,))
datasets = far_ex.Datasets.from_list(datasets)

# build up a feddforward NN calssifier
import tensorflow.contrib.layers as tcl

with tf.variable_scope('model'):
h1 = tcl.fully_connected(x, 300)
out = tcl.fully_connected(h1, datasets.train.dim_target)
print('Ground model weights (parameters)')
[print(e) for e in tf.model_variables()];
with tf.variable_scope('inital_weight_model'):
h1_hyp = tcl.fully_connected(x, 300,
variables_collections=far.HYPERPARAMETERS_COLLECTIONS,
trainable=False)
out_hyp = tcl.fully_connected(h1_hyp, datasets.train.dim_target,
variables_collections=far.HYPERPARAMETERS_COLLECTIONS,
trainable=False)
print('Initial model weights (hyperparameters)')
[print(e) for e in far.utils.hyperparameters()];
# far.utils.remove_from_collection(far.GraphKeys.MODEL_VARIABLES, *far.utils.hyperparameters())

# get an hyperparameter for weighting the examples for the inner objective loss (training error)
weights = far.get_hyperparameter('ex_weights', tf.zeros(datasets.train.num_examples))

# build loss and accuracy
# inner objective (training error), weighted mean of cross entropy errors (with sigmoid to be sure is > 0)
with tf.name_scope('errors'):
tr_loss = tf.reduce_mean(tf.sigmoid(weights)*tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=out))
# outer objective (validation error) (not weighted)
val_loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=out))
accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(y, 1), tf.argmax(out, 1)), tf.float32))

# optimizers
# get an hyperparameter for the learning rate
lr = far.get_hyperparameter('lr', 0.01)
io_optim = far.GradientDescentOptimizer(lr) # for training error minimization an optimizer from far_ho is needed
oo_optim = far.GradientOracle(lr)
#oo_optim = tf.train.AdamOptimizer() # for outer objective optimizer all optimizers from tf are valid

print('hyperparameters to optimize')
[print(h) for h in far.hyperparameters()];

# build hyperparameter optimizer
farho = far.HyperOptimizer()
run = farho.minimize(val_loss, oo_optim, tr_loss, io_optim,
init_dynamics_dict={v: h for v, h in zip(tf.model_variables(), far.utils.hyperparameters()[:4])})

print('Variables (or tensors) that will store the values of the hypergradients')
print(*far.hypergradients(), sep='\n')

# run hyperparameter optimization
T = 200 # performs 200 iteraitons of gradient descent on the training error (rise this values for better performances)
# get data suppliers (could also be stochastic for SGD)
tr_supplier = datasets.train.create_supplier(x, y)
val_supplier = datasets.validation.create_supplier(x, y)
tf.global_variables_initializer().run()

print('training accuracy', accuracy.eval(tr_supplier()))
print('validation accuracy', accuracy.eval(val_supplier()))
print('-'*50)

tr_accs, val_accs = [], []
for _ in range(20):
run(T, inner_objective_feed_dicts=tr_supplier, outer_objective_feed_dicts=val_supplier)
tr_accs.append(accuracy.eval(tr_supplier())), val_accs.append(accuracy.eval(val_supplier()))
print('training accuracy', tr_accs[-1])
print('validation accuracy', val_accs[-1])
print('learning rate', lr.eval())
print('norm of examples weight', tf.norm(weights).eval())
print('-'*50)

我在 MacO 上使用 Python 3.6 和 Tensorflow 版本 1.7.0。

提前致谢!

最佳答案

我发现了错误。 GATE_OP 必须被称为 tf.train.Optimizer.GATE_OP。否则未定义。

关于python - 名称错误 : name 'GATE_OP' is not defined #tensorflow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50779554/

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