Esempio n. 1
0
    def init_graph(self):
        if self.sess is not None:
            return
        self.graph = tf.Graph()
        with self.graph.as_default():

            tf1.set_random_seed(self.random_seed)

            self.feat_index = tf1.placeholder(tf.int32, shape=[None, None],
                                              name="feat_index")  # None * F
            self.feat_value = tf1.placeholder(tf.float32, shape=[None, None],
                                              name="feat_value")  # None * F
            self.label = tf1.placeholder(tf.float32, shape=[None, 1], name="label")  # None * 1
            self.dropout_keep_fm = tf1.placeholder(tf.float32, shape=[None], name="dropout_keep_fm")
            self.dropout_keep_deep = tf1.placeholder(tf.float32, shape=[None], name="dropout_keep_deep")
            self.train_phase = tf1.placeholder(tf.bool, name="train_phase")

            self.weights = self._initialize_weights()

            # 每一个feature 有一个 embedding
            # feature_embeddings.shape=(self.feature_size, self.embedding_size)

            # feat_index[i,j] 存储的是 第i条样本第j个field 对应的 feature_index
            # 1. 如果 field_j 是非 one hot 特征,则 field_j 不需要拆成多个 feature,
            #   feat_index[:,j] 所有样本行都是同一个值,对应同一个 feature_index。
            # 2. 如果 field_j 是 one hot 特征,则 field_j 需要拆成多个 feature,每个枚举值独立成一个 feature,
            #   此时 feat_index[:,j] 不同行是不同值,其值表示 枚举值Value(field_j) 对应的 feature_index.
            #   比如,第i=3行样本,第j=5个field表示颜色,其值是红色,红色被 onehot成 feature_index=13.则 feat_index[3,5]=13

            # shape=(N样本数量 * field_size * K)
            # N 表示样本的数量
            # K 是嵌入向量的长度,
            # 取出所有样本,每个 feature 的嵌入向量
            # 对于one_hot 的 field,相当于只取出来枚举值对应的 feature_index 的嵌入向量,
            # 相当于每个 field 取一个,最终每条样本嵌入向量的数量还是 field 。
            self.embeddings = tf.nn.embedding_lookup(
                self.weights["feature_embeddings"],  # shape=(self.feature_size, self.embedding_size)
                self.feat_index  # N * field_size
            )
            # shape=(None * F * 1)
            #
            feat_value = tf.reshape(self.feat_value, shape=[-1, self.field_size, 1])  # None * F * 1

            # FM部分的公式是 (x_i * x_j)(v_i*v_j)=(x_i*v_i)(x_j*v_j)
            # 这里先把每个特征的向量乘上其特征值。
            self.embeddings = tf.multiply(self.embeddings, feat_value)  # None * F * K

            # ---------- first order term ----------
            # 对于k维,tf.reduce_sum(x,axis=k-1)的结果是对最里面一维所有元素进行求和
            self.y_first_order = tf.nn.embedding_lookup(self.weights["feature_bias"], self.feat_index)  # None * F * 1
            self.y_first_order = tf.reduce_sum(tf.multiply(self.y_first_order, feat_value), 2)  # None * F
            self.y_first_order = tf.nn.dropout(self.y_first_order, rate=1 - self.dropout_keep_fm[0])  # None * F

            # ---------- second order term ---------------
            # sum_square part
            self.summed_features_emb = tf.reduce_sum(self.embeddings, 1)  # None * K
            self.summed_features_emb_square = tf.square(self.summed_features_emb)  # None * K

            # square_sum part
            self.squared_features_emb = tf.square(self.embeddings)
            self.squared_sum_features_emb = tf.reduce_sum(self.squared_features_emb, 1)  # None * K

            # second order
            self.y_second_order = 0.5 * tf.subtract(self.summed_features_emb_square,
                                                    self.squared_sum_features_emb)  # None * K
            self.y_second_order = tf.nn.dropout(self.y_second_order, rate=1 - self.dropout_keep_fm[1])  # None * K

            # ---------- Deep component ----------
            self.y_deep = tf.reshape(self.embeddings, shape=[-1, self.field_size * self.embedding_size])  # None * (F*K)
            self.y_deep = tf.nn.dropout(self.y_deep, rate=1 - self.dropout_keep_deep[0])
            for i in range(0, len(self.deep_layers)):
                self.y_deep = tf.add(tf.matmul(self.y_deep, self.weights["layer_%d" % i]),
                                     self.weights["bias_%d" % i])  # None * layer[i] * 1
                if self.batch_norm:
                    self.y_deep = self.batch_norm_layer(self.y_deep, train_phase=self.train_phase,
                                                        scope_bn="bn_%d" % i)  # None * layer[i] * 1
                self.y_deep = self.deep_layers_activation(self.y_deep)
                self.y_deep = tf.nn.dropout(self.y_deep,
                                            rate=1 - self.dropout_keep_deep[1 + i])  # dropout at each Deep layer

            # ---------- DeepFM ----------
            if self.use_fm and self.use_deep:
                concat_input = tf.concat([self.y_first_order, self.y_second_order, self.y_deep], axis=1)
            elif self.use_fm:
                concat_input = tf.concat([self.y_first_order, self.y_second_order], axis=1)
            elif self.use_deep:
                concat_input = self.y_deep
            self.out = tf.add(tf.matmul(concat_input, self.weights["concat_projection"]), self.weights["concat_bias"])

            # loss
            if self.loss_type == "logloss":
                self.out = tf.nn.sigmoid(self.out, name='out')
                self.loss = tf1.losses.log_loss(self.label, self.out)
            elif self.loss_type == "mse":
                self.loss = tf.nn.l2_loss(tf.subtract(self.label, self.out))
            # l2 regularization on weights 正则
            if self.l2_reg > 0:
                self.loss += tf.contrib.layers.l2_regularizer(
                    self.l2_reg)(self.weights["concat_projection"])
                if self.use_deep:
                    for i in range(len(self.deep_layers)):
                        self.loss += tf.contrib.layers.l2_regularizer(
                            self.l2_reg)(self.weights["layer_%d" % i])

            # optimizer
            # 这里可以使用现成的ftrl优化损失
            #  optimizer = tf.train.FtrlOptimizer(lr)  # lr: learningRate
            #  gradients = optimizer.compute_gradients(loss)  # cost
            #  train_op = optimizer.apply_gradients(gradients, global_step=global_step)

            if self.optimizer_type == "adam":
                self.optimizer = tf1.train.AdamOptimizer(learning_rate=self.learning_rate, beta1=0.9, beta2=0.999,
                                                         epsilon=1e-8).minimize(self.loss)
            elif self.optimizer_type == "adagrad":
                self.optimizer = tf1.train.AdagradOptimizer(learning_rate=self.learning_rate,
                                                            initial_accumulator_value=1e-8).minimize(self.loss)
            elif self.optimizer_type == "gd":
                self.optimizer = tf1.train.GradientDescentOptimizer(learning_rate=self.learning_rate).minimize(
                    self.loss)
            elif self.optimizer_type == "momentum":
                self.optimizer = tf1.train.MomentumOptimizer(learning_rate=self.learning_rate, momentum=0.95).minimize(
                    self.loss)
            # elif self.optimizer_type == "yellowfin":
            #     self.optimizer = YFOptimizer(learning_rate=self.learning_rate, momentum=0.0).minimize(
            #         self.loss)

            # init
            self.saver = tf1.train.Saver()
            init = tf1.global_variables_initializer()
            self.sess = self._init_session()
            self.sess.run(init)

            # number of params
            total_parameters = 0
            for variable in self.weights.values():
                shape = variable.get_shape()
                variable_parameters = 1
                for dim in shape:
                    variable_parameters *= dim.value
                total_parameters += variable_parameters
            if self.verbose > 0:
                print("#params: %d" % total_parameters)
from werkzeug.utils import secure_filename
import numpy
import matplotlib.pyplot as plt
from io import BytesIO
import base64
import keras
from keras.backend import clear_session
from keras.models import load_model
from keras.optimizers import SGD, RMSprop
from keras.losses import categorical_crossentropy
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from flask import jsonify
from tensorflow.compat.v1 import set_random_seed
SEED = 7
np.random.seed(SEED)
set_random_seed(SEED)

graph = tf.get_default_graph()
session = tf.Session(graph=tf.Graph())
with session.graph.as_default():
    keras.backend.set_session(session)
    classifier = load_model('resnet_2048.h5')
    classifier.load_weights('resnet_2048_weights.h5', by_name=True)
    classifier._make_predict_function()


#Pre-processing
def crop_image_from_gray(img, tol=7):
    if img.ndim == 2:
        mask = img > tol
        return img[np.ix_(mask.any(1), mask.any(0))]
def train_unconstrained(dataset,
                        group_info,
                        epsilon=0.01,
                        loops=10000,
                        skip_steps=400):
    """Train unconstrained classifier.

  Args:
    dataset: train, vali and test sets
    group_info: group memberships on train, vali and test sets and thresholds
    epsilon: constraint slack
    loops: number of gradient steps
    skip_steps: steps to skip before snapshotting metrics
  """
    tf.set_random_seed(121212)
    np.random.seed(212121)
    random.seed(333333)

    x_train, y_train, _, x_vali, y_vali, _, x_test, y_test, _ = dataset

    (group_memberships_list_train, group_memberships_list_vali,
     group_memberships_list_test, _) = group_info

    model = create_model(x_train.shape[-1])
    features_tensor = tf.constant(x_train)
    labels_tensor = tf.constant(y_train)

    predictions = lambda: model(features_tensor)
    predictions_vali = lambda: model(x_vali)
    predictions_test = lambda: model(x_test)

    context = tfco.rate_context(predictions, labels=lambda: labels_tensor)
    overall_error = tfco.error_rate(context, penalty_loss=tfco.HingeLoss())
    problem = tfco.RateMinimizationProblem(overall_error)

    loss_fn, update_ops_fn, _ = tfco.create_lagrangian_loss(problem)
    optimizer = tf.keras.optimizers.Adagrad(0.1)

    objectives_list = []
    objectives_list_test = []
    objectives_list_vali = []
    violations_list = []
    violations_list_test = []
    violations_list_vali = []
    model_weights = []

    for ii in range(loops):
        update_ops_fn()
        optimizer.minimize(loss_fn, var_list=model.trainable_weights)

        # Snapshot iterate once in 1000 loops.
        if ii % skip_steps == 0:
            pred = np.reshape(predictions(), (-1, ))
            err = error_rate(y_train, pred)
            max_viol, viol_list = violation(y_train, pred, epsilon,
                                            group_memberships_list_train)

            pred_test = np.reshape(predictions_test(), (-1, ))
            err_test = error_rate(y_test, pred_test)
            _, viol_list_test = violation(y_test, pred_test, epsilon,
                                          group_memberships_list_test)

            pred_vali = np.reshape(predictions_vali(), (-1, ))
            err_vali = error_rate(y_vali, pred_vali)
            max_viol_vali, viol_list_vali = violation(
                y_vali, pred_vali, epsilon, group_memberships_list_vali)

            objectives_list.append(err)
            objectives_list_test.append(err_test)
            objectives_list_vali.append(err_vali)
            violations_list.append(viol_list)
            violations_list_test.append(viol_list_test)
            violations_list_vali.append(viol_list_vali)
            model_weights.append(model.get_weights())

            if ii % 1000 == 0:
                print(
                    "Epoch %d | Error = %.3f | Viol = %.3f | Viol_vali = %.3f"
                    % (ii, err, max_viol, max_viol_vali),
                    flush=True)

    # Best candidate index.
    best_ind = np.argmin(objectives_list)
    model.set_weights(model_weights[best_ind])

    print("Train:")
    evaluate(x_train, y_train, model, epsilon, group_memberships_list_train)
    print("\nVali:")
    evaluate(x_vali, y_vali, model, epsilon, group_memberships_list_vali)
    print("\nTest:")
    evaluate(x_test, y_test, model, epsilon, group_memberships_list_test)
Esempio n. 4
0
    40000:], df.loc[:39999, "sentiment"].values, df.loc[40000:,
                                                        'sentiment'].values

# hyper-parameters for LSTM-RNN model
lstm_size = 128
lstm_layers = 1
batch_size = 128
l_rate = 0.001
n_epoch = 40

# Creating LSTM based RNN model using tensorflow
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

tf.reset_default_graph()
tf.set_random_seed(123)

# input placeholders
with tf.name_scope("inputs"):
    tf_x = tf.placeholder(dtype=tf.int32,
                          shape=(None, avg_seq_len),
                          name="tf_x")
    tf_y = tf.placeholder(dtype=tf.float32, shape=(None), name="tf_y")
    tf_keepprob = tf.placeholder(tf.float32, name="tf_keepprob")

# Call to the tf.nn.embedding_lookup() function in order to get our word vectors from the input placeholder
# The call to that function will return a 3-D Tensor of dimensionality batch size by max sequence length by word vector dimensions.
# we use lookup function to loop up the row in embedding matrix for each element in the placeholder tf_x
with tf.name_scope("embeddings"):
    embed_x = tf.nn.embedding_lookup(embeddings_vector, tf_x, name="embed_x")
Esempio n. 5
0
                        default=100000,
                        help="Train up to this number of steps.")
    parser.add_argument(
        "--preprocess_threads",
        type=int,
        default=8,
        help="Number of CPU threads to use for parallel decoding of training "
        "images.")
    parser.add_argument("--save_checkpoint_secs",
                        type=int,
                        default=300,
                        help="Seconds elapsed b/w saving models.")
    parser.add_argument(
        "--save_summary_secs",
        type=int,
        default=60,
        help="Seconds elapsed b/w saving tf logging summaries.")
    parser.add_argument(
        "--log_dir",
        default="/tmp/tensorboard_logs",
        help=
        "Directory for storing Tensorboard logging files; set to empty string '' to disable Tensorboard logging."
    )

    args = parser.parse_args()

    tf.set_random_seed(args.seed)
    np.random.seed(args.seed)

    train(args)
Esempio n. 6
0
import numpy as np
import gym
import tensorflow.compat.v1 as tf

tf.disable_v2_behavior()

np.random.seed(2)
tf.set_random_seed(2)


class Actor(object):
    def __init__(self, sess, n_features, action_bound, lr=0.0001):
        self.sess = sess

        self.s = tf.placeholder(tf.float32, [1, n_features], "state")
        self.a = tf.placeholder(tf.float32, None, name="act")
        self.td_error = tf.placeholder(tf.float32, None,
                                       name="td_error")  # TD_error

        l1 = tf.layers.dense(
            inputs=self.s,
            units=30,  # number of hidden units
            activation=tf.nn.relu,
            kernel_initializer=tf.random_normal_initializer(0., .1),  # weights
            bias_initializer=tf.constant_initializer(0.1),  # biases
            name='l1')

        mu = tf.layers.dense(
            inputs=l1,
            units=1,  # number of hidden units
            activation=tf.nn.tanh,
Esempio n. 7
0
 def _maybe_seed():
   if tf.executing_eagerly():
     tf1.set_random_seed(42)
     return None
   return 42
def model(X_train,
          Y_train,
          X_test,
          Y_test,
          learning_rate=0.009,
          num_epochs=100,
          minibatch_size=64,
          print_cost=True,
          isPlot=True):

    ops.reset_default_graph()
    tf.set_random_seed(1)
    seed = 3
    (m, n_H0, n_W0, n_C0) = X_train.shape
    n_y = Y_train.shape[1]
    costs = []
    # 为当前的维度创建占位符
    X, Y = create_placeholders(n_H0, n_W0, n_C0, n_y)
    # 初始化参数
    parameters = initialize_parameters()
    # 前向传播
    Z3 = forward_propagation(X, parameters)
    # 计算陈本
    cost = compute_cost(Z3, Y)

    # 反向传播,由于框架已经实现了反向传播,我们只需要选择一个优化器就可了
    optimizer = tf.train.AdamOptimizer(
        learning_rate=learning_rate).minimize(cost)

    # 全局初始化所有变量
    init = tf.global_variables_initializer()

    # 开始运行
    with tf.Session() as sess:

        # 初始化参数
        sess.run(init)
        # 开始便利数据集
        for epoch in range(num_epochs):
            minibatch_cost = 0
            num_minibatches = int(m / minibatch_size)
            seed = seed + 1
            minibatches = cnn_utils.random_mini_batches(
                X_train, Y_train, minibatch_size, seed)

            #   对每个数据块进行处理
            for minibatch in minibatches:
                (minibatch_X, minibatch_Y) = minibatch
                #   最小化这个数据化的成本(这里应该是进行一次梯度下降吧)
                _, temp_cost = sess.run([optimizer, cost],
                                        feed_dict={
                                            X: minibatch_X,
                                            Y: minibatch_Y
                                        })
                minibatch_cost += temp_cost / num_minibatches

            if print_cost:
                if epoch % 5 == 0:
                    print("当前的成本为: ", epoch, "代,成本为:" + str(minibatch_cost))

            if epoch % 5 == 0:
                costs.append(minibatch_cost)

        if isPlot:
            plt.plot(np.squeeze(costs))
            plt.ylabel('cost')
            plt.xlabel('daishu')
            plt.title("learning rate = " + str(learning_rate))
            plt.show()

        # 开始预测数据
        # 计算当前的预测情况
        predict_op = tf.arg_max(Z3, 1)
        corrent_prediction = tf.equal(predict_op, tf.arg_max(Y, 1))

        # 计算准确度
        accuracy = tf.reduce_mean(tf.cast(corrent_prediction, "float"))
        print("corrent_prediction accuracy= " + str(accuracy))

        train_accuracy = accuracy.eval({X: X_train, Y: Y_train})
        test_accuracy = accuracy.eval({X: X_test, Y: Y_test})
        print("训练集准确度: " + str(train_accuracy))
        print("测试集准确度: " + str(test_accuracy))

        return (train_accuracy, test_accuracy, parameters)
Esempio n. 9
0
from sklearn.model_selection import train_test_split


def clear():
    _ = system('clear')


# Implementation of a simple MLP network with one hidden layer. Tested on the iris data set.
# Requires: numpy, sklearn>=0.18.1, tensorflow>=1.0

# NOTE: In order to make the code simple, we rewrite x * W_1 + b_1 = x' * W_1'
# where x' = [x | 1] and W_1' is the matrix W_1 appended with a new row with elements b_1's.
# Similarly, for h * W_2 + b_2

RANDOM_SEED = 42
tf.set_random_seed(RANDOM_SEED)


def init_weights(shape):
    """ Weight initialization """
    weights = tf.random_normal(shape, stddev=0.1)
    return tf.Variable(weights)


def forwardprop(X, w_1, w_2):
    """
    Forward-propagation.
    IMPORTANT: yhat is not softmax since TensorFlow's softmax_cross_entropy_with_logits() does that internally.
    """
    h = tf.nn.sigmoid(tf.matmul(X, w_1))  # The \sigma function
    yhat = tf.matmul(h, w_2)  # The \varphi function
Esempio n. 10
0
    def test_vimco_and_gradient(self):
        dims = 5  # Dimension
        num_draws = int(1e3)
        num_batch_draws = int(3)
        seed = test_util.test_seed()

        with tf.GradientTape(persistent=True) as tape:
            f = lambda logu: tfp.vi.kl_reverse(logu, self_normalized=False)
            np_f = lambda logu: -logu

            s = tf.constant(1.)
            tape.watch(s)
            p = tfd.MultivariateNormalFullCovariance(covariance_matrix=tridiag(
                dims, diag_value=1, offdiag_value=0.5))

            # Variance is very high when approximating Forward KL, so we make
            # scale_diag large. This ensures q "covers" p and thus Var_q[p/q] is
            # smaller.
            q = tfd.MultivariateNormalDiag(scale_diag=tf.tile([s], [dims]))

            vimco = tfp.vi.csiszar_vimco(f=f,
                                         p_log_prob=p.log_prob,
                                         q=q,
                                         num_draws=num_draws,
                                         num_batch_draws=num_batch_draws,
                                         seed=seed)

            # We want the seed to be the same since we will use computations
            # with the same underlying sample to show correctness of vimco.
            if tf.executing_eagerly():
                tf1.set_random_seed(seed)
            x = q.sample(sample_shape=[num_draws, num_batch_draws], seed=seed)
            x = tf.stop_gradient(x)
            logu = p.log_prob(x) - q.log_prob(x)
            f_log_sum_u = f(tfp.stats.log_soomean_exp(logu, axis=0)[::-1][0])
            q_log_prob_x = q.log_prob(x)

        grad_vimco = tape.gradient(vimco, s)
        grad_mean_f_log_sum_u = tape.gradient(f_log_sum_u, s) / num_batch_draws
        jacobian_logqx = tape.jacobian(q_log_prob_x, s)

        [
            logu_,
            jacobian_logqx_,
            vimco_,
            grad_vimco_,
            f_log_sum_u_,
            grad_mean_f_log_sum_u_,
        ] = self.evaluate([
            logu,
            jacobian_logqx,
            vimco,
            grad_vimco,
            f_log_sum_u,
            grad_mean_f_log_sum_u,
        ])

        np_log_avg_u, np_log_sooavg_u = self._csiszar_vimco_helper(logu_)

        # Test VIMCO loss is correct.
        self.assertAllClose(np_f(np_log_avg_u).mean(axis=0),
                            vimco_,
                            rtol=1e-4,
                            atol=1e-5)

        # Test gradient of VIMCO loss is correct.
        #
        # To make this computation we'll inject two gradients from TF:
        # - grad[mean(f(log(sum(p(x)/q(x)))))]
        # - jacobian[log(q(x))].
        #
        # We now justify why using these (and only these) TF values for
        # ground-truth does not undermine the completeness of this test.
        #
        # Regarding `grad_mean_f_log_sum_u_`, note that we validate the
        # correctness of the zero-th order derivative (for each batch member).
        # Since `tfp.vi.csiszar_vimco_helper` itself does not manipulate any
        # gradient information, we can safely rely on TF.
        self.assertAllClose(np_f(np_log_avg_u),
                            f_log_sum_u_,
                            rtol=1e-4,
                            atol=1e-5)
        #
        # Regarding `jacobian_logqx_`, note that testing the gradient of
        # `q.log_prob` is outside the scope of this unit-test thus we may safely
        # use TF to find it.

        # The `mean` is across batches and the `sum` is across iid samples.
        np_grad_vimco = (grad_mean_f_log_sum_u_ + np.mean(np.sum(
            jacobian_logqx_ * (np_f(np_log_avg_u) - np_f(np_log_sooavg_u)),
            axis=0),
                                                          axis=0))

        self.assertAllClose(np_grad_vimco, grad_vimco_, rtol=0.03, atol=1e-3)
Esempio n. 11
0
  def train_a_model(input_seq, mask_seq, label_seq, vocab_size, d_model, head, init_weights, print_output=True):
    # Clear all stuffs in default graph, so we can start fresh
    tf.reset_default_graph()

    with tf.device(USED_DEVICE):
      # We want each session to have different random seed, but we need each run to have the same random sequence
      tf.set_random_seed(random.randint(0, 65535))

      batch_size = len(input_seq[0])
      seq_len = len(input_seq[0][0])

      sess = setup_tensorflow_session()
      (input_tensor, mask_tensor, output_tensor, disagreement_cost, logprob_tensor) = build_model(batch=batch_size, seq_len=seq_len, vocab_size=vocab_size, d_model=d_model, head=head)
      (label_tensor, train_op, loss, classification_loss) = build_train_graph(output_tensor=output_tensor, batch=batch_size, seq_len=seq_len, d_model=d_model, additional_costs=[disagreement_cost])
      sess.run(tf.global_variables_initializer())

      if init_weights is not None:
        set_all_variables(sess, init_weights)

      for i in range(LOCAL_TRAIN_EPOCH):
        if print_output:
          print('EPOCH: ' + str(i))

        avg_loss = 0.0
        avg_disagreement_loss = 0.0
        avg_classification_loss = 0.0
        avg_accuracy = 0.0
        for input_sample, mask_sample, label_sample in zip(input_seq, mask_seq, label_seq):
          [output_vals, loss_vals, disagreement_cost_vals, classification_loss_vals, logprob_vals, _] = sess.run([output_tensor, loss, disagreement_cost, classification_loss, logprob_tensor, train_op], feed_dict={input_tensor: input_sample, mask_tensor: mask_sample, label_tensor: label_sample})
          avg_loss = avg_loss + loss_vals
          avg_disagreement_loss = avg_disagreement_loss + disagreement_cost_vals
          avg_classification_loss = avg_classification_loss + classification_loss_vals
          labels = np.array(label_sample)
          predictions = (logprob_vals >= 0.5).astype(int)
          scores = (predictions == labels).astype(int)
          scores = np.average(scores)
          avg_accuracy = avg_accuracy + scores
        avg_loss = avg_loss / len(input_seq)
        avg_disagreement_loss = avg_disagreement_loss / len(input_seq)
        avg_classification_loss = avg_classification_loss / len(input_seq)
        avg_accuracy = avg_accuracy / len(input_seq)

      if print_output:
        print('===== TRAIN STAT =====')
        '''
        print('=== Input Values ===')
        print(input_seq)
        print('=== Label Values ===')
        print(label_seq)
        print('=== Output Values ===')
        print(output_vals)
        '''
        print('=== Loss Values ===')
        print(avg_loss)
        print('=== Classification Loss Values ===')
        print(avg_classification_loss)
        '''
        print('=== Disagreement Loss Values ===')
        print(avg_disagreement_loss)
        '''
        print('=== Accuracy ===')
        print(avg_accuracy)

      trained_weights = get_all_variables(sess)
      return [avg_loss, avg_disagreement_loss, avg_classification_loss, avg_accuracy, trained_weights]
Esempio n. 12
0
    def test_a_model(input_seq,
                     mask_seq,
                     label_seq,
                     var_list,
                     d_model,
                     head,
                     print_output=False):
        # Clear all stuffs in default graph, so we can start fresh
        tf.reset_default_graph()

        with tf.device(USED_DEVICE):
            # We want each session to have different random seed, but we need each run to have the same random sequence
            tf.set_random_seed(random.randint(0, 65535))

            batch_size = len(input_seq[0])
            seq_len = len(input_seq[0][0])

            sess = setup_tensorflow_session()
            (input_tensor, mask_tensor, output_tensor, disagreement_cost,
             logprob_tensor) = build_model(batch=batch_size,
                                           seq_len=seq_len,
                                           vocab_size=VOCAB_SIZE,
                                           d_model=d_model,
                                           head=head)
            (label_tensor, loss, classification_loss) = build_loss_graph(
                output_tensor=output_tensor,
                batch=batch_size,
                seq_len=seq_len,
                d_model=d_model,
                additional_costs=[disagreement_cost])
            sess.run(tf.global_variables_initializer())
            set_all_variables(sess, var_list)

            avg_loss = 0.0
            avg_disgreement_loss = 0.0
            avg_classification_loss = 0.0
            avg_accuracy = 0.0
            for input_sample, mask_sample, label_sample in zip(
                    input_seq, mask_seq, label_seq):
                [
                    output_vals, loss_vals, disagreement_cost_vals,
                    classification_loss_vals, logprob_vals
                ] = sess.run(
                    [
                        output_tensor, loss, disagreement_cost,
                        classification_loss, logprob_tensor
                    ],
                    feed_dict={
                        input_tensor: input_sample,
                        mask_tensor: mask_sample,
                        label_tensor: label_sample
                    })

                print(
                    '----------------------------------------------------------------------'
                )
                for input_ids, output_v, label_v in zip(
                        input_sample, logprob_vals, label_sample):
                    input_decoded = decode_input_ids(input_ids)
                    print(' --> ' + str(input_decoded) + ' => ' +
                          str(output_v) + '/' + str(label_v))
                print(
                    '----------------------------------------------------------------------'
                )

                avg_loss = avg_loss + loss_vals
                avg_disgreement_loss = avg_disgreement_loss + disagreement_cost_vals
                avg_classification_loss = avg_classification_loss + classification_loss_vals
                labels = np.array(label_sample)
                predictions = (logprob_vals >= 0.5).astype(int)
                scores = (predictions == labels).astype(int)
                scores = np.average(scores)
                avg_accuracy = avg_accuracy + scores
            avg_loss = avg_loss / len(input_seq)
            avg_disgreement_loss = avg_disgreement_loss / len(input_seq)
            avg_classification_loss = avg_classification_loss / len(input_seq)
            avg_accuracy = avg_accuracy / len(input_seq)

            if print_output:
                print('=== Input Values ===')
                print(input_seq)
                print('=== Label Values ===')
                print(label_seq)
                print('=== Output Values ===')
                print(output_vals)
                print('=== Loss Values ===')
                print(avg_loss)
                print('=== Classification Loss Values ===')
                print(avg_classification_loss)
                print('=== Disagreement Loss Values ===')
                print(avg_disgreement_loss)
                print('=== Accuracy ===')
                print(avg_accuracy)
            return avg_loss, avg_disgreement_loss, avg_classification_loss, avg_accuracy
Esempio n. 13
0
# Increase reproducibility
from numpy.random import seed
seed(1)
from tensorflow.compat.v1 import set_random_seed
set_random_seed(2)
import torch
torch.manual_seed(0)

import tensorflow as tf
import numpy as np
import pandas as pd
import csv
import os

# Own
import configs
import data
import pos
import bert
import models
from train import train
import utils
import features

print(tf.__version__)
print(tf.keras.__version__)

# setting device on GPU if available, else CPU
T_DEVICE = 'cpu'
if T_DEVICE == 'cpu':
    device = torch.device(T_DEVICE)
# import tensorflow as tf
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

tf.set_random_seed(777)  # for reproducibility

#1. 그래프 구현#

# H(x) = Wx + b
# X and Y data, H(x) = Y
#x_train = [1, 2, 3]
#y_train = [1, 2, 3]
X = tf.placeholder(tf.float32, shape=[None])
Y = tf.placeholder(tf.float32, shape=[None])
#                                   1차원이고 값은 마음데로 넣을 수 있다.

W = tf.Variable(tf.random_normal([1]), name="weight")
b = tf.Variable(tf.random_normal([1]), name="bias")
#                                rank

# Our hypothesis XW+b
hypothesis = X * W + b

# cost/loss function
cost = tf.reduce_mean(tf.square(hypothesis - Y))

#t = [1. , 2. , 3. , 4.]
#tf.reduce_mean(t) ==> 2.5 평균을 내주는 명령어

# GradientDescent (Minimize/optimizer)
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01)
Esempio n. 15
0
# Paper 12
# Predicting two classes only (Normal v COVID-19)

from numpy.random import seed
import math
seed(42)
from tensorflow.compat.v1 import set_random_seed
set_random_seed(42)
# libraries
from keras.preprocessing.image import ImageDataGenerator
import keras
from sklearn.metrics import classification_report, confusion_matrix
import numpy as np
from keras.models import Model 
from keras.layers import Dense, Input, Dropout, Flatten
from sklearn.metrics import roc_auc_score
from sklearn.metrics import roc_auc_score
from sklearn.metrics import roc_curve, auc, roc_auc_score
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score

# GPU
from tensorflow.compat.v1 import ConfigProto
from tensorflow.compat.v1 import InteractiveSession
import tensorflow as tf 

tf.keras.backend.clear_session()

config = ConfigProto()
config.gpu_options.allow_growth = True
gpu_options = tf.compat.v1.GPUOptions(per_process_gpu_memory_fraction=0.333)
Esempio n. 16
0
def get_graph(sess,
              seed,
              edges,
              vertices,
              adj,
              labels,
              source,
              sink,
              reward_states,
              env,
              other_sources=[],
              other_sinks=[]):

    # def get_graph(seed,sess,reward_states,env,itera):

    row, col = env.grid.shape

    eachepoch = FLAGS.eachepoch  # Plot the resulting VF after each epoch or not

    features = np.eye(
        len(vertices), dtype=np.int32
    )  # One-hot encoding for the features (i.e. feature-less)
    features = sparse_to_tuple(sp.lil_matrix(features))

    np.random.seed(seed)
    tf.set_random_seed(seed)

    # features, adj, labels,\
    #  vertices, edges, row, col, source,\
    #   sink, other_sinks, obs2grid, grid2obs = load_data(append=FLAGS.app)

    # reward_states =  map(grid2obs.get,reward_states)

    # pdb.set_trace()
    y_train, y_val,\
     train_mask, val_mask = get_splits(labels, source, sink, reward_states)

    if sink is None:
        print("Sink not there, skipping this seed.")
        return None, None

    # Compute some baselines
    # start = time.time()
    # sc = SpectralClustering(2, affinity='precomputed',
    #                         n_init=5, eigen_solver='arpack', assign_labels='discretize')
    # sc.fit(adj.toarray())
    # print('Time for spectral clustering {}'.format(time.time()-start))

    # start = time.time()
    # cut = nx.minimum_cut(G,source,sink)
    # print("Total time for mincut {}".format(time.time()-start))

    # Some preprocessing
    # features = preprocess_features(features)
    if FLAGS.model == 'gcn':
        support = [preprocess_adj(adj)]
        num_supports = 1
        model_func = GCN
    elif FLAGS.model == 'gcn_cheby':
        support = chebyshev_polynomials(adj, FLAGS.max_degree)
        num_supports = 1 + FLAGS.max_degree
        model_func = GCN
    elif FLAGS.model == 'dense':
        support = [preprocess_adj(adj)]  # Not used
        num_supports = 1
        model_func = MLP
    else:
        raise ValueError('Invalid argument for model: ' + str(FLAGS.model))
    # adj=normalize_adj(adj).toarray()
    # pdb.set_trace()
    adj = adj.toarray()
    deg = np.diag(np.sum(adj, axis=1))
    laplacian = deg - adj

    # Define placeholders
    placeholders = {
        'adj':
        tf.placeholder(tf.float32,
                       shape=(None, None)),  #unnormalized adjancy matrix
        'support':
        [tf.sparse_placeholder(tf.float32) for _ in range(num_supports)],
        'features':
        tf.sparse_placeholder(tf.float32,
                              shape=tf.constant(features[2], dtype=tf.int64)),
        'labels':
        tf.placeholder(tf.float32, shape=(None, y_train.shape[1])),
        'labels_mask':
        tf.placeholder(tf.int32),
        'dropout':
        tf.placeholder_with_default(0., shape=()),
        'num_features_nonzero':
        tf.placeholder(tf.int32),  # helper variable for sparse dropout
        'learning_rate':
        tf.placeholder(tf.float32)
    }

    model = model_func(placeholders,
                       edges,
                       laplacian,
                       input_dim=features[2][1],
                       logging=True,
                       FLAGS=FLAGS)

    sess.run(tf.global_variables_initializer())

    feed_dict = construct_feed_dict(adj, features, support, y_train,
                                    train_mask, placeholders)
    feed_dict.update({placeholders['learning_rate']: FLAGS.learning_rate})

    cost_val = []

    start = time.time()
    for epoch in range(FLAGS.epochs):

        if eachepoch:
            outputs = sess.run([tf.nn.softmax(model.outputs)],
                               feed_dict=feed_dict)[0]

            X = np.ones((row * col)) * -0.11
            Xround = np.ones((row * col)) * .5
            X[vertices] = outputs[:, 1]
            walls = np.where(env.grid.flatten() == 1)
            X[walls] = -0.1
            Xround[vertices] = np.round(X[vertices])
            if (Xround[vertices] == 0.).all():
                X[0] = Xround[0] = 1.
            X = X.reshape(row, col)
            Xround = Xround.reshape(row, col)

            # path = np.ones((row*col))*0.25
            # path[vertices] = .5
            # path_sources = map(obs2grid.get, other_sources)
            # path[path_sources] = 0.
            # path_sinks = map(obs2grid.get, other_sinks)
            # path[path_sinks] = 1.
            # path=path.reshape(row,col)

            # A=map(obs2grid.get,cut[1][0])
            # B=map(obs2grid.get,cut[1][1])
            # view_cut=np.ones((row*col))*0.5
            # view_cut[A] = 0
            # view_cut[B] = 1
            # view_cut=view_cut.reshape(row,col)

            # pdb.set_trace()

            # A=map(obs2grid.get,np.argwhere(sc.labels_ ==1).flatten() )
            # B=map(obs2grid.get,np.argwhere(sc.labels_ ==0).flatten() )

            # n_cut=np.ones((row*col))*0.5
            # n_cut[A] = 0
            # n_cut[B] = 1
            # n_cut=n_cut.reshape(row,col)
            # pdb.set_trace()

            # if featplot is not None:
            #     fig, ax = plt.subplots(6,1)
            #     ax[0].imshow(path, interpolation='nearest')
            #     ax[1].imshow(featplot.reshape(row,col), interpolation='nearest')
            #     ax[2].imshow(Xround, interpolation='nearest')
            #     ax[3].imshow(X, interpolation='nearest')
            #     ax[4].imshow(view_cut, interpolation='nearest')
            #     ax[5].imshow(n_cut, interpolation='nearest')

            # else:
            mask_map = np.ones((row * col)) * -0.11
            mask_map[vertices] = y_train[:, 1]
            mask_map = mask_map.reshape(row, col)

            # fig, ax = plt.subplots(5,1)
            # ax[0].imshow(path, interpolation='nearest')
            # ax[1].imshow(Xround, interpolation='nearest')
            # ax[2].imshow(X, interpolation='nearest')
            # ax[3].imshow(view_cut, interpolation='nearest')
            # ax[4].imshow(n_cut, interpolation='nearest')
            fig, ax = plt.subplots(2, 1)
            # ax[0].imshow(path, interpolation='nearest')
            ax[0].imshow(X, interpolation='nearest')
            ax[1].imshow(mask_map, interpolation='nearest', cmap=new_map)

            # plt.show()
            # plt.close()

            directory = "diff_{}_{}/".format(FLAGS.app, row * col)
            if not os.path.exists(directory):
                os.makedirs(directory)
            plt.savefig("{}seed{}_{}.png".format(directory, seed, epoch))
            plt.close()
            plt.clf()

        t = time.time()
        # pdb.set_trace()
        feed_dict = construct_feed_dict(adj, features, support, y_train,
                                        train_mask, placeholders)
        feed_dict.update({placeholders['learning_rate']: FLAGS.learning_rate})
        outs = sess.run(
            [model.opt_op, model.loss, model.accuracy, model.learning_rate],
            feed_dict=feed_dict)

    print("Total time for gcn {}".format(time.time() - start))
    print("Optimization Finished!")

    if not eachepoch:

        outputs = sess.run([tf.nn.softmax(model.outputs)],
                           feed_dict=feed_dict)[0]

        X = np.ones((row * col)) * -0.11
        Xround = np.ones((row * col)) * .5
        X[vertices] = outputs[:, 1]
        walls = np.where(env.grid.flatten() == 1)
        X[walls] = -0.1
        Xround[vertices] = np.round(X[vertices])
        if (Xround[vertices] == 0.).all():
            X[0] = Xround[0] = 1.
        X = X.reshape(row, col)
        Xround = Xround.reshape(row, col)

        mask_map = np.ones((row * col)) * -0.11
        mask_map[vertices] = y_train[:, 1]
        mask_map = mask_map.reshape(row, col)

        # fig, ax = plt.subplots(1)
        # ax.imshow(X, interpolation='nearest',cmap=new_map)
        # # ax[1].imshow(mask_map, interpolation='nearest',cmap=new_map)
        # # plt.show()
        # # plt.close()
        # # sys.exit()

        # directory = "{}_{}/".format(FLAGS.app,row*col)
        # if not os.path.exists(directory):
        #     os.makedirs(directory)

        # if FLAGS.fig:
        #     myfig = '_'+FLAGS.fig + "_{}".format(epoch)
        # else:
        #     myfig = ''

        # # plt.title('Diffusion-Based Approximate Value Function')
        # plt.xticks([])
        # plt.yticks([])
        # plt.savefig("{}seed{}_{}.png".format(directory,seed,myfig),bbox_inches='tight')
        # plt.close()
        # # pdb.set_trace()
        # sys.exit()

    # pdb.set_trace()
    initiation_set = list(np.argwhere(Xround.flatten()[vertices] == 0))
    goals = np.argwhere(Xround.flatten()[vertices] == 1)

    # if len(initiation_set) > 1:
    #     initiation_set = map(obs2grid.get,initiation_set.squeeze())
    # if len(goals) > 1:
    #     goals = map(obs2grid.get,goals.squeeze())
    V_weights = outputs[:, 1]

    # pdb.set_trace()

    # sinks = list(np.argsort(X.flatten())[::-1])
    # sinks = [sink for sink in sinks if sink in goals]

    # pdb.set_trace()

    tf.reset_default_graph()
    return initiation_set, V_weights
Esempio n. 17
0
def main(argv):
  if len(argv) > 1:
    raise app.UsageError("Too many command-line arguments.")
 
  if FLAGS.seed is None:
    seed = np.random.randint(low=0, high=1e5)
  else:
    seed = FLAGS.seed
  np.random.seed(seed)
  random.seed(seed)
  tf.set_random_seed(seed)
  game = pyspiel.load_game_as_turn_based(FLAGS.game_name,
                                         {"players": pyspiel.GameParameter(
                                             FLAGS.n_players)})
  env = rl_environment.Environment(game,seed=seed)
  env.reset()

  if FLAGS.heuristic_list:
    heuristic_list = FLAGS.heuristic_list
    if '_strategy' in heuristic_list[0]:
      FLAGS.meta_strategy_method = heuristic_list[0][:heuristic_list[0].index('_strategy')]
    else:
      FLAGS.meta_strategy_method = heuristic_list[0]
  else:
    heuristic_list = ["general_nash_strategy", "uniform_strategy"] 
  
  if 'sp' in FLAGS.heuristic_to_add:
    heuristic_list.append("self_play_strategy")
  if 'weighted_ne' in FLAGS.heuristic_to_add:
    heuristic_list.append("weighted_NE_strategy")
  if 'prd' in FLAGS.heuristic_to_add:
    heuristic_list.append("prd_strategy")
  
  if not os.path.exists(FLAGS.root_result_folder):
    os.makedirs(FLAGS.root_result_folder)
  
  checkpoint_dir = 'se_'+FLAGS.game_name+str(FLAGS.n_players)+'_sims_'+str(FLAGS.sims_per_entry)+'_it_'+str(FLAGS.gpsro_iterations)+'_ep_'+str(FLAGS.number_training_episodes)+'_or_'+FLAGS.oracle_type

  checkpoint_dir += '_msl_'+",".join(heuristic_list)

  if FLAGS.switch_fast_slow:
    checkpoint_dir += '_sfs_'+'_fp_'+str(FLAGS.fast_oracle_period)+'_sp_'+str(FLAGS.slow_oracle_period) + '_arslr_'+str(FLAGS.ars_learning_rate)+'_arsn_'+str(FLAGS.noise)+'_arsnd_'+str(FLAGS.num_directions)+'_arsbd_'+str(FLAGS.num_best_directions)+'_epars_'+str(FLAGS.number_training_episodes_ars)
  elif FLAGS.switch_heuristic_regardless_of_oracle:
    checkpoint_dir += '_switch_heuristics_'

  if FLAGS.oracle_type == 'BR':
    oracle_flag_str = ''
  else:
    oracle_flag_str = '_hl_'+str(FLAGS.hidden_layer_size)+'_bs_'+str(FLAGS.batch_size)+'_nhl_'+str(FLAGS.n_hidden_layers)
    if FLAGS.oracle_type == 'DQN':
      oracle_flag_str += '_dqnlr_'+str(FLAGS.dqn_learning_rate)+'_tnuf_'+str(FLAGS.update_target_network_every)+'_lf_'+str(FLAGS.learn_every)
    else:
      oracle_flag_str += '_ls_'+str(FLAGS.loss_str)+'_nqbp_'+str(FLAGS.num_q_before_pi)+'_ec_'+str(FLAGS.entropy_cost)+'_clr_'+str(FLAGS.critic_learning_rate)+'_pilr_'+str(FLAGS.pi_learning_rate)
  checkpoint_dir = checkpoint_dir + oracle_flag_str+'_se_'+str(seed)+'_'+datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
  checkpoint_dir = os.path.join(os.getcwd(),FLAGS.root_result_folder, checkpoint_dir)
                                
  writer = SummaryWriter(logdir=checkpoint_dir+'/log')
  if FLAGS.sbatch_run:
    sys.stdout = open(checkpoint_dir+'/stdout.txt','w+')

  # Initialize oracle and agents
  with tf.Session() as sess:
    if FLAGS.oracle_type == "DQN":
      slow_oracle, agents, agent_kwargs = init_dqn_responder(sess, env)
    elif FLAGS.oracle_type == "PG":
      slow_oracle, agents, agent_kwargs = init_pg_responder(sess, env)
    elif FLAGS.oracle_type == "BR":
      slow_oracle, agents = init_br_responder(env)
      agent_kwargs = None
    elif FLAGS.oracle_type == "ARS":
      slow_oracle, agents = init_ars_responder(sess, env)
      agent_kwargs = None

    sess.run(tf.global_variables_initializer())
    
    if FLAGS.switch_fast_slow:
      fast_oracle, agents = init_ars_responder(sess=None, env=env)
      oracle_list = [[], []]
      oracle_list[0].append(slow_oracle)
      oracle_list[0].append(fast_oracle)
      oracle_list[1] = [FLAGS.oracle_type,'ARS']
    else:
      oracle_list = None

    gpsro_looper(env,
                 slow_oracle,
                 oracle_list,
                 agents,
                 writer,
                 quiesce=FLAGS.quiesce,
                 checkpoint_dir=checkpoint_dir,
                 seed=seed,
                 heuristic_list=heuristic_list)


  writer.close()
Esempio n. 18
0
def main(argv):
    del argv  # Unused.

    tf.enable_resource_variables()
    tf.set_random_seed(FLAGS.seed)
    set_lr_schedule()
    set_custom_sparsity_map()
    folder_stub = os.path.join(FLAGS.training_method, str(FLAGS.end_sparsity),
                               str(FLAGS.maskupdate_begin_step),
                               str(FLAGS.maskupdate_end_step),
                               str(FLAGS.maskupdate_frequency),
                               str(FLAGS.drop_fraction),
                               str(FLAGS.label_smoothing),
                               str(FLAGS.weight_decay))

    output_dir = FLAGS.output_dir
    if FLAGS.use_folder_stub:
        output_dir = os.path.join(output_dir, folder_stub)

    export_dir = os.path.join(output_dir, 'export_dir')

    # we pass the updated eval and train string to the params dictionary.
    params = {}
    params['output_dir'] = output_dir
    params['training_method'] = FLAGS.training_method
    params['use_tpu'] = FLAGS.use_tpu

    dataset_func = functools.partial(
        imagenet_input.ImageNetInput,
        data_dir=FLAGS.data_directory,
        transpose_input=False,
        num_parallel_calls=FLAGS.num_parallel_calls,
        use_bfloat16=False)
    imagenet_train, imagenet_eval = [
        dataset_func(is_training=is_training) for is_training in [True, False]
    ]

    run_config = tpu_config.RunConfig(
        master=FLAGS.master,
        model_dir=output_dir,
        save_checkpoints_steps=FLAGS.steps_per_checkpoint,
        keep_checkpoint_max=FLAGS.keep_checkpoint_max,
        session_config=tf.ConfigProto(allow_soft_placement=True,
                                      log_device_placement=False),
        tpu_config=tpu_config.TPUConfig(
            iterations_per_loop=FLAGS.iterations_per_loop,
            num_shards=FLAGS.num_cores,
            tpu_job_name=FLAGS.tpu_job_name))

    classifier = tpu_estimator.TPUEstimator(
        use_tpu=FLAGS.use_tpu,
        model_fn=resnet_model_fn_w_pruning,
        params=params,
        config=run_config,
        train_batch_size=FLAGS.train_batch_size,
        eval_batch_size=FLAGS.eval_batch_size)

    cpu_classifier = tpu_estimator.TPUEstimator(
        use_tpu=FLAGS.use_tpu,
        model_fn=resnet_model_fn_w_pruning,
        params=params,
        config=run_config,
        train_batch_size=FLAGS.train_batch_size,
        export_to_tpu=False,
        eval_batch_size=FLAGS.eval_batch_size)

    if FLAGS.num_eval_images % FLAGS.eval_batch_size != 0:
        raise ValueError(
            'eval_batch_size (%d) must evenly divide num_eval_images(%d)!' %
            (FLAGS.eval_batch_size, FLAGS.num_eval_images))

    eval_steps = FLAGS.num_eval_images // FLAGS.eval_batch_size
    if FLAGS.mode == 'eval_once':
        ckpt_path = os.path.join(output_dir, FLAGS.eval_once_ckpt_prefix)
        dataset = imagenet_train if FLAGS.eval_on_train else imagenet_eval
        classifier.evaluate(input_fn=dataset.input_fn,
                            steps=eval_steps,
                            checkpoint_path=ckpt_path,
                            name='{0}'.format(FLAGS.eval_once_ckpt_prefix))
    elif FLAGS.mode == 'eval':
        # Run evaluation when there's a new checkpoint
        for ckpt in evaluation.checkpoints_iterator(output_dir):
            tf.logging.info('Starting to evaluate.')
            try:
                dataset = imagenet_train if FLAGS.eval_on_train else imagenet_eval
                classifier.evaluate(input_fn=dataset.input_fn,
                                    steps=eval_steps,
                                    checkpoint_path=ckpt,
                                    name='eval')
                # Terminate eval job when final checkpoint is reached
                global_step = int(os.path.basename(ckpt).split('-')[1])
                if global_step >= FLAGS.train_steps:
                    tf.logging.info(
                        'Evaluation finished after training step %d' %
                        global_step)
                    break

            except tf.errors.NotFoundError:
                logging('Checkpoint no longer exists,skipping checkpoint.')

    else:
        global_step = estimator._load_global_step_from_checkpoint_dir(
            output_dir)
        # Session run hooks to export model for prediction
        export_hook = ExportModelHook(cpu_classifier, export_dir)
        hooks = [export_hook]

        if FLAGS.mode == 'train':
            tf.logging.info('start training...')
            classifier.train(input_fn=imagenet_train.input_fn,
                             hooks=hooks,
                             max_steps=FLAGS.train_steps)
        else:
            assert FLAGS.mode == 'train_and_eval'
            tf.logging.info('start training and eval...')
            while global_step < FLAGS.train_steps:
                next_checkpoint = min(global_step + FLAGS.steps_per_eval,
                                      FLAGS.train_steps)
                classifier.train(input_fn=imagenet_train.input_fn,
                                 max_steps=next_checkpoint)
                global_step = next_checkpoint
                logging('Completed training up to step :', global_step)
                classifier.evaluate(input_fn=imagenet_eval.input_fn,
                                    steps=eval_steps)
Esempio n. 19
0
def score(neurones, epoques=2000, ng=1, nd=1, lr=0.001, verbose=False):
    scores = []
    for nb_neur in neurones:
        couches = len(nb_neur)
        tf.reset_default_graph()
        tf.set_random_seed(1234)
        X = tf.placeholder(tf.float32, [None, X_size])
        Z = tf.placeholder(tf.float32, [None, X_size])

        gen_sample = generator(Z, nb_neurone=nb_neur, couches=couches)
        z_sample = encoder(X, nb_neurone=nb_neur, couches=couches)

        real_output = discriminator(X,
                                    z_sample,
                                    nb_neurone=nb_neur,
                                    couches=couches)
        fake_output = discriminator(gen_sample,
                                    Z,
                                    nb_neurone=nb_neur,
                                    couches=couches,
                                    reuse=True)

        #Discriminator loss
        disc_loss = -tf.reduce_mean(
            tf.log(real_output + 1e-5) + tf.log(1.0 - fake_output + 1e-5))

        #Generator loss
        gen_loss = -tf.reduce_mean(
            tf.log(fake_output + 1e-5) + tf.log(1.0 - real_output + 1e-5))

        #Define the Optimizer with learning rate  0.001

        gen_vars = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES,
                                     scope="BIGAN/Generator")

        disc_vars = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES,
                                      scope="BIGAN/Discriminator")

        enc_vars = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES,
                                     scope="BIGAN/Encoder")

        gen_step = tf.train.RMSPropOptimizer(learning_rate=0.001).minimize(
            gen_loss, var_list=gen_vars + enc_vars)
        disc_step = tf.train.RMSPropOptimizer(learning_rate=0.001).minimize(
            disc_loss, var_list=disc_vars)

        sess = tf.Session()
        sess.run(tf.global_variables_initializer())
        nd_steps = 1  #entrainer plus de dis que gen
        ng_steps = 1
        d_loss_list = []
        g_loss_list = []

        with tf.device('/device:GPU:0'):
            for i in range(epoques):
                Z_batch = sample_noise_Gaus(Y_size, X_size)
                #ind_X = random.sample(range(Y_size),Y_size)
                for _ in range(nd_steps):
                    _, dloss = sess.run([disc_step, disc_loss],
                                        feed_dict={
                                            X: X_batch,
                                            Z: Z_batch
                                        })

                for _ in range(ng_steps):
                    _, gloss = sess.run([gen_step, gen_loss],
                                        feed_dict={
                                            X: X_batch,
                                            Z: Z_batch
                                        })

                if i % 100 == 0:
                    print("Iterations:", i, "Discriminator loss: ", dloss,
                          "Generator loss:", gloss)
                    d_loss_list.append(dloss)
                    g_loss_list.append(gloss)
        score = 0
        """
            for _ in range(1000):
            Z_test = sample_noise_Gaus(np.shape(X_test)[0],X_size)
            pred_test=sess.run(gen_sample,feed_dict={Z: Z_test})
            score = score + KDE(pred_test,X_test)
            score = score/1000
            """
        Z_test = sample_noise_Gaus(np.shape(X_test)[0], X_size)
        pred_test = sess.run(gen_sample, feed_dict={Z: Z_test})
        score = KDE(pred_test, X_test)
        print("Score pour C:", couches, " N:", nb_neur, ", score:", score)
        nom = "C" + str(couches) + "N" + str(nb_neur)
        tmp = [nom, score]
        scores.append(tmp)

        Z_batch = sample_noise_Gaus(Y_size, X_size)
        pred = sess.run(gen_sample, feed_dict={Z: Z_batch})
        D0 = pd.DataFrame(np.transpose((X_batch[:, 0], pred[:, 0])))
        D0.columns = ['real', 'fake']
        D0.plot.density()
        #plt.ylim((-0.05, 0.4))
        #plt.xlim((-25, 25))
        plt.title('return series of stock 1')
    return scores
Esempio n. 20
0
    def train(self):

        # Create session
        tfconfig = tf.ConfigProto(allow_soft_placement=True)
        tfconfig.gpu_options.allow_growth = True
        sess = tf.Session(config=tfconfig)

        with sess.graph.as_default():

            tf.set_random_seed(cfg.FLAGS.rng_seed)
            layers = self.net.create_architecture(sess,
                                                  "TRAIN",
                                                  self.imdb.num_classes,
                                                  tag='default')
            loss = layers['total_loss']
            lr = tf.Variable(cfg.FLAGS.learning_rate, trainable=False)
            momentum = cfg.FLAGS.momentum
            optimizer = tf.train.MomentumOptimizer(lr, momentum)

            gvs = optimizer.compute_gradients(loss)

            # Double bias
            # Double the gradient of the bias if set
            if cfg.FLAGS.double_bias:
                final_gvs = []
                with tf.variable_scope('Gradient_Mult'):
                    for grad, var in gvs:
                        scale = 1.
                        if cfg.FLAGS.double_bias and '/biases:' in var.name:
                            scale *= 2.
                        if not np.allclose(scale, 1.0):
                            grad = tf.multiply(grad, scale)
                        final_gvs.append((grad, var))
                train_op = optimizer.apply_gradients(final_gvs)
            else:
                train_op = optimizer.apply_gradients(gvs)

            # We will handle the snapshots ourselves
            self.saver = tf.train.Saver(max_to_keep=100000)
            # Write the train and validation information to tensorboard
            # writer = tf.summary.FileWriter(self.tbdir, sess.graph)
            # valwriter = tf.summary.FileWriter(self.tbvaldir)

        # Load weights
        # Fresh train directly from ImageNet weights
        print('Loading initial model weights from {:s}'.format(
            cfg.FLAGS.pretrained_model))
        variables = tf.global_variables()
        # Initialize all variables first
        sess.run(tf.variables_initializer(variables, name='init'))
        var_keep_dic = self.get_variables_in_checkpoint_file(
            cfg.FLAGS.pretrained_model)
        # Get the variables to restore, ignorizing the variables to fix
        variables_to_restore = self.net.get_variables_to_restore(
            variables, var_keep_dic)

        restorer = tf.train.Saver(variables_to_restore)
        restorer.restore(sess, cfg.FLAGS.pretrained_model)
        print('Loaded.')
        # Need to fix the variables before loading, so that the RGB weights are changed to BGR
        # For VGG16 it also changes the convolutional weights fc6 and fc7 to
        # fully connected weights
        self.net.fix_variables(sess, cfg.FLAGS.pretrained_model)
        print('Fixed.')
        sess.run(tf.assign(lr, cfg.FLAGS.learning_rate))
        last_snapshot_iter = 0

        timer = Timer()
        #iter = last_snapshot_iter + 1
        iter = last_snapshot_iter
        last_summary_time = time.time()
        while iter < cfg.FLAGS.max_iters + 1:
            # Learning rate
            if iter == cfg.FLAGS.step_size + 1:
                # Add snapshot here before reducing the learning rate
                # self.snapshot(sess, iter)
                sess.run(
                    tf.assign(lr, cfg.FLAGS.learning_rate * cfg.FLAGS.gamma))

            timer.tic()
            # Get training data, one batch at a time
            blobs = self.data_layer.forward()

            # Compute the graph without summary
            try:
                rpn_loss_cls, rpn_loss_box, loss_cls, loss_box, total_loss = self.net.train_step(
                    sess, blobs, train_op)
            except Exception:
                # if some errors were encountered image is skipped without increasing iterations
                print('image invalid, skipping')
                continue

            timer.toc()
            #iter += 1

            # Display training information
            if iter % (cfg.FLAGS.display) == 0:
                print('iter: %d / %d, total loss: %.6f\n >>> rpn_loss_cls: %.6f\n '
                      '>>> rpn_loss_box: %.6f\n >>> loss_cls: %.6f\n >>> loss_box: %.6f\n ' % \
                      (iter, cfg.FLAGS.max_iters, total_loss, rpn_loss_cls, rpn_loss_box, loss_cls, loss_box))
                print('speed: {:.3f}s / iter'.format(timer.average_time))

            if iter % cfg.FLAGS.snapshot_iterations == 0:
                self.snapshot(sess, iter)

            iter += 1
Esempio n. 21
0
def main(unused_argv):
    # pylint:disable=unused-variable
    # Reason:
    #   This training script relys on many programmatical call to function and
    #   access to variables. Pylint cannot infer this case so it emits false alarm
    #   of unused-variable if we do not disable this warning.

    # pylint:disable=invalid-name
    # Reason:
    #   Following variables have their name consider to be invalid by pylint so
    #   we disable the warning.
    #   - Variable that in its name has A or B indictating their belonging of
    #     one side of data.
    del unused_argv

    # Load main config
    config_name = FLAGS.config
    config = load_config(config_name)

    config_name_A = config['config_A']
    config_name_B = config['config_B']
    config_name_classifier_A = config['config_classifier_A']
    config_name_classifier_B = config['config_classifier_B']

    # Load dataset
    dataset_A = common_joint.load_dataset(config_name_A, FLAGS.exp_uid_A)
    (dataset_blob_A, train_data_A, train_label_A, train_mu_A, train_sigma_A,
     index_grouped_by_label_A) = dataset_A
    dataset_B = common_joint.load_dataset(config_name_B, FLAGS.exp_uid_B)
    (dataset_blob_B, train_data_B, train_label_B, train_mu_B, train_sigma_B,
     index_grouped_by_label_B) = dataset_B

    # Prepare directories
    dirs = common_joint.prepare_dirs('joint', config_name, FLAGS.exp_uid)
    save_dir, sample_dir = dirs

    # Set random seed
    np.random.seed(FLAGS.random_seed)
    tf.set_random_seed(FLAGS.random_seed)

    # Real Training.
    tf.reset_default_graph()
    sess = tf.Session()

    # Load model's architecture (= build)
    one_side_helper_A = common_joint.OneSideHelper(config_name_A,
                                                   FLAGS.exp_uid_A,
                                                   config_name_classifier_A,
                                                   FLAGS.exp_uid_classifier)
    one_side_helper_B = common_joint.OneSideHelper(config_name_B,
                                                   FLAGS.exp_uid_B,
                                                   config_name_classifier_B,
                                                   FLAGS.exp_uid_classifier)
    m = common_joint.load_model(model_joint.Model, config_name, FLAGS.exp_uid)

    # Prepare summary
    train_writer = tf.summary.FileWriter(save_dir + '/transfer_train',
                                         sess.graph)
    scalar_summaries = tf.summary.merge([
        tf.summary.scalar(key, value)
        for key, value in m.get_summary_kv_dict().items()
    ])
    manual_summary_helper = common_joint.ManualSummaryHelper()

    # Initialize and restore
    sess.run(tf.global_variables_initializer())

    one_side_helper_A.restore(dataset_blob_A)
    one_side_helper_B.restore(dataset_blob_B)

    # Miscs from config
    batch_size = config['batch_size']
    n_latent_shared = config['n_latent_shared']
    pairing_number = config['pairing_number']
    n_latent_A = config['vae_A']['n_latent']
    n_latent_B = config['vae_B']['n_latent']
    i_start = 0
    # Data iterators

    single_data_iterator_A = common_joint.SingleDataIterator(
        train_mu_A, train_sigma_A, batch_size)
    single_data_iterator_B = common_joint.SingleDataIterator(
        train_mu_B, train_sigma_B, batch_size)
    paired_data_iterator = common_joint.PairedDataIterator(
        train_mu_A, train_sigma_A, train_data_A, train_label_A,
        index_grouped_by_label_A, train_mu_B, train_sigma_B, train_data_B,
        train_label_B, index_grouped_by_label_B, pairing_number, batch_size)
    single_data_iterator_A_for_evaluation = common_joint.SingleDataIterator(
        train_mu_A, train_sigma_A, batch_size)
    single_data_iterator_B_for_evaluation = common_joint.SingleDataIterator(
        train_mu_B, train_sigma_B, batch_size)

    # Training loop
    n_iters = FLAGS.n_iters
    for i in tqdm(list(range(i_start, n_iters)),
                  desc='training',
                  unit=' batch'):
        # Prepare data for this batch
        # - Unsupervised (A)
        x_A, _ = next(single_data_iterator_A)
        x_B, _ = next(single_data_iterator_B)
        # - Supervised (aligning)
        x_align_A, x_align_B, align_debug_info = next(paired_data_iterator)
        real_x_align_A, real_x_align_B = align_debug_info

        # Run training op and write summary
        res = sess.run(
            [m.train_full, scalar_summaries], {
                m.x_A: x_A,
                m.x_B: x_B,
                m.x_align_A: x_align_A,
                m.x_align_B: x_align_B,
            })
        train_writer.add_summary(res[-1], i)

        if i % FLAGS.n_iters_per_save == 0:
            # Save the model if instructed
            config_name = FLAGS.config
            model_uid = common.get_model_uid(config_name, FLAGS.exp_uid)

            save_name = os.path.join(save_dir,
                                     'transfer_%s_%d.ckpt' % (model_uid, i))
            m.vae_saver.save(sess, save_name)
            with tf.gfile.Open(os.path.join(save_dir, 'ckpt_iters.txt'),
                               'w') as f:
                f.write('%d' % i)

        # Evaluate if instructed
        if i % FLAGS.n_iters_per_eval == 0:
            # Helper functions
            def joint_sample(sample_size):
                z_hat = np.random.randn(sample_size, n_latent_shared)
                return sess.run([m.x_joint_A, m.x_joint_B], {
                    m.z_hat: z_hat,
                })

            def get_x_from_prior_A():
                return sess.run(m.x_from_prior_A)

            def get_x_from_prior_B():
                return sess.run(m.x_from_prior_B)

            def get_x_from_posterior_A():
                return next(single_data_iterator_A_for_evaluation)[0]

            def get_x_from_posterior_B():
                return next(single_data_iterator_B_for_evaluation)[0]

            def get_x_prime_A(x_A):
                return sess.run(m.x_prime_A, {m.x_A: x_A})

            def get_x_prime_B(x_B):
                return sess.run(m.x_prime_B, {m.x_B: x_B})

            def transfer_A_to_B(x_A):
                return sess.run(m.x_A_to_B, {m.x_A: x_A})

            def transfer_B_to_A(x_B):
                return sess.run(m.x_B_to_A, {m.x_B: x_B})

            def manual_summary(key, value):
                summary = manual_summary_helper.get_summary(sess, key, value)
                # This [cell-var-from-loop] is intented
                train_writer.add_summary(summary, i)  # pylint: disable=cell-var-from-loop

            # Classifier based evaluation
            sample_total_size = 10000
            sample_batch_size = 100

            def pred(one_side_helper, x):
                real_x = six.ensure_text(one_side_helper.m_helper, x)
                return one_side_helper.m_classifier_helper.classify(
                    real_x, batch_size)

            def accuarcy(x_1, x_2, type_1, type_2):
                assert type_1 in ('A', 'B') and type_2 in ('A', 'B')
                func_A = partial(pred, one_side_helper=one_side_helper_A)
                func_B = partial(pred, one_side_helper=one_side_helper_B)
                func_1 = func_A if type_1 == 'A' else func_B
                func_2 = func_A if type_2 == 'A' else func_B
                pred_1, pred_2 = func_1(x=x_1), func_2(x=x_2)
                return np.mean(np.equal(pred_1, pred_2).astype('f'))

            def joint_sample_accuarcy():
                x_A, x_B = joint_sample(sample_size=sample_total_size)  # pylint: disable=cell-var-from-loop
                return accuarcy(x_A, x_B, 'A', 'B')

            def transfer_sample_accuarcy_A_B():
                x_A = get_x_from_prior_A()
                x_B = transfer_A_to_B(x_A)
                return accuarcy(x_A, x_B, 'A', 'B')

            def transfer_sample_accuarcy_B_A():
                x_B = get_x_from_prior_B()
                x_A = transfer_B_to_A(x_B)
                return accuarcy(x_A, x_B, 'A', 'B')

            def transfer_accuarcy_A_B():
                x_A = get_x_from_posterior_A()
                x_B = transfer_A_to_B(x_A)
                return accuarcy(x_A, x_B, 'A', 'B')

            def transfer_accuarcy_B_A():
                x_B = get_x_from_posterior_B()
                x_A = transfer_B_to_A(x_B)
                return accuarcy(x_A, x_B, 'A', 'B')

            def recons_accuarcy_A():
                # Use x_A in outer scope
                # These [cell-var-from-loop]s are intended
                x_A_prime = get_x_prime_A(x_A)  # pylint: disable=cell-var-from-loop
                return accuarcy(x_A, x_A_prime, 'A', 'A')  # pylint: disable=cell-var-from-loop

            def recons_accuarcy_B():
                # use x_B in outer scope
                # These [cell-var-from-loop]s are intended
                x_B_prime = get_x_prime_B(x_B)  # pylint: disable=cell-var-from-loop
                return accuarcy(x_B, x_B_prime, 'B', 'B')  # pylint: disable=cell-var-from-loop

            # Do all manual summary
            for func_name in (
                    'joint_sample_accuarcy',
                    'transfer_sample_accuarcy_A_B',
                    'transfer_sample_accuarcy_B_A',
                    'transfer_accuarcy_A_B',
                    'transfer_accuarcy_B_A',
                    'recons_accuarcy_A',
                    'recons_accuarcy_B',
            ):
                func = locals()[func_name]
                manual_summary(func_name, func())

            # Sampling based evaluation / sampling
            x_prime_A = get_x_prime_A(x_A)
            x_prime_B = get_x_prime_B(x_B)
            x_from_prior_A = get_x_from_prior_A()
            x_from_prior_B = get_x_from_prior_B()
            x_A_to_B = transfer_A_to_B(x_A)
            x_B_to_A = transfer_B_to_A(x_B)
            x_align_A_to_B = transfer_A_to_B(x_align_A)
            x_align_B_to_A = transfer_B_to_A(x_align_B)
            x_joint_A, x_joint_B = joint_sample(sample_size=batch_size)

            this_iter_sample_dir = os.path.join(sample_dir,
                                                'transfer_train_sample',
                                                '%010d' % i)
            tf.gfile.MakeDirs(this_iter_sample_dir)

            for helper, var_names, x_is_real_x in [
                (one_side_helper_A.m_helper,
                 ('x_A', 'x_prime_A', 'x_from_prior_A', 'x_B_to_A',
                  'x_align_A', 'x_align_B_to_A', 'x_joint_A'), False),
                (one_side_helper_A.m_helper, ('real_x_align_A', ), True),
                (one_side_helper_B.m_helper,
                 ('x_B', 'x_prime_B', 'x_from_prior_B', 'x_A_to_B',
                  'x_align_B', 'x_align_A_to_B', 'x_joint_B'), False),
                (one_side_helper_B.m_helper, ('real_x_align_B', ), True),
            ]:
                for var_name in var_names:
                    # Here `var` would be None if
                    #   - there is no such variable in `locals()`, or
                    #   - such variable exists but the value is None
                    # In both case, we would skip saving data from it.
                    var = locals().get(var_name, None)
                    if var is not None:
                        helper.save_data(var, var_name, this_iter_sample_dir,
                                         x_is_real_x)
Esempio n. 22
0
 def setUp(self):
   super(Transformer2dTest, self).setUp()
   tf.set_random_seed(self.SEED)
   np.random.seed(self.SEED)
Esempio n. 23
0
def main(argv):
    del argv  # unused here
    gcs_root = "gs://v1net-tpu-bucket/"
    args = FLAGS.flag_values_dict()
    gcs_path = "gs://v1net-tpu-bucket/%s/" % args["base_dir"]
    model_dir = os.path.join(gcs_path,
                             "model_dir_%s" % args["experiment_name"])
    summaries_dir = os.path.join(gcs_path, "summaries")
    if not tf.gfile.Exists(model_dir):
        tf.gfile.MakeDirs(model_dir)
    if not tf.gfile.Exists(model_dir):
        tf.gfile.MakeDirs(summaries_dir)
    args["model_dir"] = model_dir
    args["summaries_dir"] = summaries_dir

    rand_seed = np.random.randint(10000)
    tf.set_random_seed(rand_seed)
    args["random_seed"] = rand_seed
    warm_start_settings = None
    args["data_dir"] = os.path.join(gcs_root, args["data_dir"])

    num_train_examples, input_fn_train = get_input_fn_train(args)

    args["num_train_examples"] = num_train_examples * args["num_epochs"]
    args["num_train_steps"] = args["num_train_examples"] // args[
        "train_batch_size"]
    num_train_steps = args["num_train_steps"]
    num_train_steps_per_epoch = num_train_steps // args["num_epochs"]
    args["num_train_steps_per_epoch"] = num_train_steps_per_epoch

    warm_start_settings = tf.estimator.WarmStartSettings(
        ckpt_to_initialize_from=args['checkpoint'],
        vars_to_warm_start=[
            "^(?!.*side_output|.*v1net|.*Momentum|global_step|beta*|gamma*|.*Adam)"
        ],
    )

    tpu_cluster_resolver = tf.distribute.cluster_resolver.TPUClusterResolver(
        args["tpu_name"] if args["use_tpu"] else "",
        zone=args["tpu_zone"],
        project="desalab-tpu")
    tpu_config = tf.estimator.tpu.TPUConfig(
        num_shards=args["num_cores"],
        iterations_per_loop=args["iterations_per_loop"])
    config = tf.estimator.tpu.RunConfig(
        cluster=tpu_cluster_resolver,
        model_dir=model_dir,
        tpu_config=tpu_config,
        save_checkpoints_steps=args["iterations_per_loop"],
        keep_checkpoint_max=20)

    classifier = tf.estimator.tpu.TPUEstimator(
        use_tpu=args["use_tpu"],
        model_fn=model_fn,
        config=config,
        params=args,
        warm_start_from=warm_start_settings,
        train_batch_size=args["train_batch_size"],
    )

    start_timestamp = time.time()
    classifier.train(input_fn=input_fn_train, max_steps=num_train_steps)
    elapsed_time = int(time.time() - start_timestamp)
    tf.logging.info("Finished training up to step %d. Elapsed seconds %d.",
                    num_train_steps, elapsed_time)
Esempio n. 24
0
 def reinitialize_params(self, seed):
     tf.set_random_seed(seed)
     with self.graph.as_default():
         self.sess.run(tf.global_variables_initializer())
         model_params = self.get_params()
     return model_params
View more on my tutorial page: https://morvanzhou.github.io/tutorials/

Using:
Tensorflow: 1.0
gym: 0.8.0
"""
import functools
import numpy as np
#import tensorflow as tf
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
import tensorflow_probability as tfp

# reproducible
np.random.seed(1)
tf.set_random_seed(1)


class PolicyGradient:
    def __init__(self,
                 n_actions,
                 n_features,
                 learning_rate=0.01,
                 reward_decay=0.95,
                 output_graph=False,
                 policy_name='p'):
        self.policy_name = policy_name
        self.n_actions = n_actions
        self.n_features = n_features
        self.lr = learning_rate
        self.gamma = reward_decay
Esempio n. 26
0
def check_and_run_attack(dataset, config, adversarial, mixed):

    clear_session()

    # Set seed
    tf.set_random_seed(config['random_seed'])
    np.random.seed(config['random_seed'])

    # Get model
    model_file = None
    if adversarial:
        if mixed:
            model_file = tf.train.latest_checkpoint(
                config['model_dir_adv_mixed'])
        else:
            model_file = tf.train.latest_checkpoint(config['model_dir_adv'])
    else:
        if mixed:
            model_file = tf.train.latest_checkpoint(config['model_dir_mixed'])
        else:
            model_file = tf.train.latest_checkpoint(config['model_dir'])
    if model_file is None:
        print('No model found')
        sys.exit()

    # Get adversarial dataset
    path = ""
    if adversarial:
        if mixed:
            path = "attack-adv-mixed.npy"
        else:
            path = "attack-adv.npy"
    else:
        if mixed:
            path = "attack-normal-mixed.npy"
        else:
            path = "attack-normal.npy"

    x_adv = np.load(path)

    clean_acc = 0
    robust_acc = 0

    if model_file is None:
        print('No checkpoint found')
    # elif x_adv.shape != (2000, 3):
    #   print('Invalid shape: expected (10000,784), found {}'.format(x_adv.shape))
    # elif np.amax(x_adv) > 1.0001 or \
    #      np.amin(x_adv) < -0.0001 or \
    #      np.isnan(np.amax(x_adv)):
    #   print('Invalid pixel range. Expected [0, 1], found [{}, {}]'.format(
    #                                                             np.amin(x_adv),
    #                                                             np.amax(x_adv)))
    else:
        clean_acc = run_attack(model_file,
                               dataset,
                               x_adv,
                               config,
                               adv_testing=False,
                               mixed_dataset=mixed)
        robust_acc = run_attack(model_file,
                                dataset,
                                x_adv,
                                config,
                                adv_testing=True,
                                mixed_dataset=mixed)

    return (clean_acc, robust_acc)
Esempio n. 27
0
#!/usr/local/bin/python
from __future__ import division
from __future__ import print_function

import time
import tensorflow.compat.v1 as tf

from utils import *
from models import BGCN

# Set random seed

seed = 123
np.random.seed(seed)
tf.set_random_seed(seed)

# Settings
flags = tf.app.flags
FLAGS = flags.FLAGS
flags.DEFINE_string('dataset', 'citeseer', 'Dataset string.')
flags.DEFINE_string('model', 'bgcn', 'Model string.')
flags.DEFINE_float('learning_rate', 0.005, 'Initial learning rate.')
flags.DEFINE_integer('epochs', 2000, 'Number of epochs to train.')
flags.DEFINE_integer('hidden1', 16, 'Number of units in hidden layer 1.')
flags.DEFINE_integer('patience', 100, 'early stop #epoch')
flags.DEFINE_float('dropout', 0.6, 'Dropout rate (1 - keep probability).')
flags.DEFINE_float('weight_decay', 1e-4,
                   'Weight for L2 loss on embedding matrix.')
'''
0.1-0.9 is the value of alpha, indicating which one is more biased when the 
two models are combined. For example, with alpha=0.9, the combined model means
Esempio n. 28
0
# Purpose: Mimics a benign agent in the federated learning setting and sets up the master agent 
########################
import warnings

warnings.filterwarnings("ignore")
import os

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

import logging
tf.get_logger().setLevel(logging.ERROR)

import numpy as np
tf.set_random_seed(777)
np.random.seed(777)
from utils.mnist import model_mnist
from utils.census_utils import census_model_1
from utils.cifar_utils import cifar10_model

from utils.eval_utils import eval_minimal

import global_vars as gv


# gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=gv.mem_frac)


def agent(i, X_shard, Y_shard, t, gpu_id, return_dict, X_test, Y_test, lr=None):
    tf.keras.backend.set_learning_phase(1)
def train_constrained(dataset,
                      group_info,
                      epsilon=0.01,
                      learning_rate=0.1,
                      dual_scale=5.0,
                      loops=10000,
                      feature_dependent_multiplier=True,
                      hidden_layers=None,
                      skip_steps=400):
    """Train constrained classifier wth Lagrangian model.

  Args:
    dataset: train, vali and test sets
    group_info: group memberships on train, vali and test sets and thresholds
    epsilon: constraint slack
    learning_rate: learning rate for theta
    dual_scale: learning rate for gamma = dual_scale * learning_rate
    loops: number of gradient steps
    feature_dependent_multiplier: should the multiplier model be feature
      dependent. If False, a common multipler is used for all constraints
    hidden_layers: list of hidden layer nodes to be used for multiplier model
    skip_steps: steps to skip before snapshotting metrics
  """
    tf.set_random_seed(121212)
    np.random.seed(212121)
    random.seed(333333)

    x_train, y_train, z_train, x_vali, y_vali, _, x_test, y_test, _ = dataset

    (group_memberships_list_train, group_memberships_list_vali,
     group_memberships_list_test,
     group_memberships_thresholds_train) = group_info

    # Models and group thresholds tensor.
    model = create_model(x_train.shape[-1])
    multiplier_model, multiplier_weights = create_multiplier_model(
        feature_dependent_multiplier=feature_dependent_multiplier,
        dim=3,
        hidden_layers=hidden_layers)
    group_thresholds = tf.Variable(np.ones(3) * 0.1, dtype=tf.float32)

    # Features, labels, predictions, multipliers.
    features_tensor = tf.constant(x_train)
    labels_tensor = tf.constant(y_train)
    features_tensor_vali = tf.constant(x_vali)

    predictions = lambda: model(features_tensor)
    predictions_vali = lambda: model(features_tensor_vali)
    predictions_test = lambda: model(x_test)

    def multiplier_values():
        return tf.abs(
            multiplier_model(tf.reshape(group_thresholds, shape=(1, -1))))

    # Lagrangian loss function.
    def lagrangian_loss():
        # Separate out objective, constraints and proxy constraints.
        objective = problem.objective()
        constraints = problem.constraints()
        proxy_constraints = problem.proxy_constraints()

        # Set-up custom Lagrangian loss.
        primal = objective
        multipliers = multiplier_values()
        primal += tf.stop_gradient(multipliers) * proxy_constraints
        dual = dual_scale * multipliers * tf.stop_gradient(constraints)
        return primal - dual

    # Objective.
    context = tfco.rate_context(predictions, labels=lambda: labels_tensor)
    overall_error = tfco.error_rate(context)

    # Slice and subset group predictions and labels.
    def group_membership():
        return (z_train[:, 0] > group_thresholds[0]) & (
            z_train[:, 1] > group_thresholds[1]) & (z_train[:, 2] >
                                                    group_thresholds[2])

    def group_predictions():
        pred = predictions()
        groups = tf.reshape(group_membership(), (-1, 1))
        return pred[groups]

    def group_labels():
        groups = tf.reshape(group_membership(), (-1, ))
        return labels_tensor[groups]

    # Constraint.
    group_context = tfco.rate_context(group_predictions, labels=group_labels)
    group_error = tfco.error_rate(group_context)
    constraints = [group_error <= overall_error + epsilon]

    # Set up constrained optimization problem and optimizer.
    problem = tfco.RateMinimizationProblem(overall_error, constraints)
    optimizer = tf.keras.optimizers.Adagrad(learning_rate)
    var_list = model.trainable_weights + multiplier_weights

    objectives_list = []
    objectives_list_test = []
    objectives_list_vali = []
    violations_list = []
    violations_list_test = []
    violations_list_vali = []
    model_weights = []

    # Training
    for ii in range(loops):
        # Sample a group membership at random.
        random_index = np.random.randint(
            group_memberships_thresholds_train.shape[0])
        group_thresholds.assign(
            group_memberships_thresholds_train[random_index, :])

        # Gradient op.
        problem.update_ops()
        optimizer.minimize(lagrangian_loss, var_list=var_list)

        # Snapshot iterate once in 1000 loops.
        if ii % skip_steps == 0:
            pred = np.reshape(predictions(), (-1, ))
            err = error_rate(y_train, pred)
            max_viol, viol_list = violation(y_train, pred, epsilon,
                                            group_memberships_list_train)

            pred_test = np.reshape(predictions_test(), (-1, ))
            err_test = error_rate(y_test, pred_test)
            _, viol_list_test = violation(y_test, pred_test, epsilon,
                                          group_memberships_list_test)

            pred_vali = np.reshape(predictions_vali(), (-1, ))
            err_vali = error_rate(y_vali, pred_vali)
            max_viol_vali, viol_list_vali = violation(
                y_vali, pred_vali, epsilon, group_memberships_list_vali)

            objectives_list.append(err)
            objectives_list_test.append(err_test)
            objectives_list_vali.append(err_vali)
            violations_list.append(viol_list)
            violations_list_test.append(viol_list_test)
            violations_list_vali.append(viol_list_vali)
            model_weights.append(model.get_weights())

            if ii % 1000 == 0:
                print(
                    "Epoch %d | Error = %.3f | Viol = %.3f | Viol_vali = %.3f"
                    % (ii, err, max_viol, max_viol_vali),
                    flush=True)

    # Best candidate index.
    best_ind = tfco.find_best_candidate_index(np.array(objectives_list),
                                              np.array(violations_list),
                                              rank_objectives=False)
    model.set_weights(model_weights[best_ind])

    print("Train:")
    evaluate(x_train, y_train, model, epsilon, group_memberships_list_train)
    print("\nVali:")
    evaluate(x_vali, y_vali, model, epsilon, group_memberships_list_vali)
    print("\nTest:")
    evaluate(x_test, y_test, model, epsilon, group_memberships_list_test)
import random
from sklearn.model_selection import train_test_split
from tensorflow.keras.optimizers import Adam, Nadam
import cv2
import numpy as np
import pandas as pd
import multiprocessing
from albumentations import Compose, VerticalFlip, HorizontalFlip, Rotate, GridDistortion
import matplotlib.pyplot as plt
from IPython.display import Image
from tqdm import tqdm_notebook as tqdm
from numpy.random import seed
import argparse
seed(10)
from tensorflow.compat.v1 import set_random_seed
set_random_seed(10)
import segmentation_models as sm
import util
import preprocessing
from datagenerator import DataGenerator
import loss
import matplotlib.pyplot as plt
from tensorflow.keras.callbacks import Callback, ModelCheckpoint
from PrAucCallback import PrAucCallback

parser = argparse.ArgumentParser()
parser.add_argument('--train_path',default='../data/train_images/', help='path to the train images')
parser.add_argument('--test_path',default='../data/test_images/', help='path to the test images')
parser.add_argument('--label_path',default='../data/train.csv', help='path to the label(trian.csv')
parser.add_argument('--sub_path',default='../data/sample_submission.csv', help='path to the submission file')
parser.add_argument('--out_path',default='../result/1104_efficientnetb3', help='path to the submission file')