本文整理匯總了Python中tensorflow.python.platform.flags.DEFINE_bool方法的典型用法代碼示例。如果您正苦於以下問題:Python flags.DEFINE_bool方法的具體用法?Python flags.DEFINE_bool怎麽用?Python flags.DEFINE_bool使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類tensorflow.python.platform.flags
的用法示例。
在下文中一共展示了flags.DEFINE_bool方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: main
# 需要導入模塊: from tensorflow.python.platform import flags [as 別名]
# 或者: from tensorflow.python.platform.flags import DEFINE_bool [as 別名]
def main(model_name, model_type):
np.random.seed(0)
assert keras.backend.backend() == "tensorflow"
set_mnist_flags()
with tf.device('/gpu:0'):
flags.DEFINE_bool('NUM_EPOCHS', args.epochs, 'Number of epochs')
# Get MNIST test data
X_train, Y_train, X_test, Y_test = data_mnist()
data_gen = data_gen_mnist(X_train)
x = K.placeholder((None,
FLAGS.IMAGE_ROWS,
FLAGS.IMAGE_COLS,
FLAGS.NUM_CHANNELS
))
y = K.placeholder(shape=(None, FLAGS.NUM_CLASSES))
model = model_mnist(type=model_type)
print(model.summary())
# Train an MNIST model
tf_train(x, y, model, X_train, Y_train, data_gen, None, None)
# Finally print the result!
_, _, test_error = tf_test_error_rate(model, x, X_test, Y_test)
print('Test error: %.1f%%' % test_error)lsm
save_model(model, model_name)
json_string = model.to_json()
with open(model_name+'.json', 'wr') as f:
f.write(json_string)
示例2: main
# 需要導入模塊: from tensorflow.python.platform import flags [as 別名]
# 或者: from tensorflow.python.platform.flags import DEFINE_bool [as 別名]
def main(model_name, model_type):
np.random.seed(0)
assert keras.backend.backend() == "tensorflow"
set_mnist_flags()
flags.DEFINE_bool('NUM_EPOCHS', args.epochs, 'Number of epochs')
# Get MNIST test data
X_train, Y_train, X_test, Y_test = data_mnist()
data_gen = data_gen_mnist(X_train)
x = K.placeholder((None,
FLAGS.IMAGE_ROWS,
FLAGS.IMAGE_COLS,
FLAGS.NUM_CHANNELS
))
y = K.placeholder(shape=(None, FLAGS.NUM_CLASSES))
model = model_mnist(type=model_type)
# Train an MNIST model
tf_train(x, y, model, X_train, Y_train, data_gen)
# Finally print the result!
test_error = tf_test_error_rate(model, x, X_test, Y_test)
print('Test error: %.1f%%' % test_error)
save_model(model, model_name)
json_string = model.to_json()
with open(model_name+'.json', 'wr') as f:
f.write(json_string)
示例3: main
# 需要導入模塊: from tensorflow.python.platform import flags [as 別名]
# 或者: from tensorflow.python.platform.flags import DEFINE_bool [as 別名]
def main(model_name, adv_model_names, model_type):
np.random.seed(0)
assert keras.backend.backend() == "tensorflow"
set_mnist_flags()
flags.DEFINE_bool('NUM_EPOCHS', args.epochs, 'Number of epochs')
# Get MNIST test data
X_train, Y_train, X_test, Y_test = data_mnist()
data_gen = data_gen_mnist(X_train)
x = K.placeholder(shape=(None,
FLAGS.IMAGE_ROWS,
FLAGS.IMAGE_COLS,
FLAGS.NUM_CHANNELS))
y = K.placeholder(shape=(FLAGS.BATCH_SIZE, FLAGS.NUM_CLASSES))
eps = args.eps
norm = args.norm
# if src_models is not None, we train on adversarial examples that come
# from multiple models
adv_models = [None] * len(adv_model_names)
ens_str = ''
for i in range(len(adv_model_names)):
adv_models[i] = load_model(adv_model_names[i])
if len(adv_models)>0:
name = basename(adv_model_names[i])
model_index = name.replace('model','')
ens_str += model_index
model = model_mnist(type=model_type)
x_advs = [None] * (len(adv_models) + 1)
for i, m in enumerate(adv_models + [model]):
if args.iter == 0:
logits = m(x)
grad = gen_grad(x, logits, y, loss='training')
x_advs[i] = symbolic_fgs(x, grad, eps=eps)
elif args.iter == 1:
x_advs[i] = iter_fgs(m, x, y, steps = 40, alpha = 0.01, eps = args.eps)
# Train an MNIST model
tf_train(x, y, model, X_train, Y_train, data_gen, x_advs=x_advs, benign = args.ben)
# Finally print the result!
test_error = tf_test_error_rate(model, x, X_test, Y_test)
print('Test error: %.1f%%' % test_error)
model_name += '_' + str(eps) + '_' + str(norm) + '_' + ens_str
if args.iter == 1:
model_name += 'iter'
if args.ben == 0:
model_name += '_nob'
save_model(model, model_name)
json_string = model.to_json()
with open(model_name+'.json', 'wr') as f:
f.write(json_string)
示例4: main
# 需要導入模塊: from tensorflow.python.platform import flags [as 別名]
# 或者: from tensorflow.python.platform.flags import DEFINE_bool [as 別名]
def main(model_name, adv_model_names, model_type):
np.random.seed(0)
assert keras.backend.backend() == "tensorflow"
set_mnist_flags()
flags.DEFINE_bool('NUM_EPOCHS', args.epochs, 'Number of epochs')
# Get MNIST test data
X_train, Y_train, X_test, Y_test = data_mnist()
data_gen = data_gen_mnist(X_train)
x = K.placeholder(shape=(None,
FLAGS.IMAGE_ROWS,
FLAGS.IMAGE_COLS,
FLAGS.NUM_CHANNELS))
y = K.placeholder(shape=(FLAGS.BATCH_SIZE, FLAGS.NUM_CLASSES))
eps = args.eps
# if src_models is not None, we train on adversarial examples that come
# from multiple models
adv_models = [None] * len(adv_model_names)
for i in range(len(adv_model_names)):
adv_models[i] = load_model(adv_model_names[i])
model = model_mnist(type=model_type)
x_advs = [None] * (len(adv_models) + 1)
for i, m in enumerate(adv_models + [model]):
logits = m(x)
grad = gen_grad(x, logits, y, loss='training')
x_advs[i] = symbolic_fgs(x, grad, eps=eps)
# Train an MNIST model
tf_train(x, y, model, X_train, Y_train, data_gen, x_advs=x_advs)
# Finally print the result!
test_error = tf_test_error_rate(model, x, X_test, Y_test)
print('Test error: %.1f%%' % test_error)
save_model(model, model_name)
json_string = model.to_json()
with open(model_name+'.json', 'wr') as f:
f.write(json_string)