def enc_graph(settings, enc_model, gen_model, tdw_img, latent_code_input):
    z_dim = latent_code_input.shape[1] + settings.latent_noise_dim
    input_shape = tdw_img.shape[1:]
    c_dim = latent_code_input.shape[1]
    
    z_mean_enc, _ = enc_model(tdw_img, z_dim, i=1)
    if settings.gen_model_name=='adaIn':
        G_dec_e = gen_model(z_mean_enc[:,:c_dim], z_mean_enc[:, c_dim:], input_shape, reuse=True)
    else:
        G_dec_e = gen_model(z_mean_enc, input_shape, reuse=True)
    z_mu = tf.gather(z_mean_enc, settings.latent_code_indices, axis=1)

    if settings.enc_loss_name=='reconstr_ce':
        reconstr_loss2_pix = 64 * 64 * 1 * metrics.binary_crossentropy(G_dec_e, tdw_img)
        eloss = tf.reduce_mean(reconstr_loss2_pix)
    elif settings.enc_loss_name=='reconstr_mse':
        reconstr_loss2_pix = metrics.mean_squared_error(G_dec_e, tdw_img)
        eloss = tf.reduce_mean(reconstr_loss2_pix)
    elif settings.enc_loss_name=='supervised_mse':
        supervised_mse_loss = metrics.mean_squared_error(z_mu, latent_code_input)
        eloss = tf.reduce_mean(supervised_mse_loss) 
    else:
        raise NotImplementedError

    return eloss, G_dec_e
def eva_matrics(y_true, y_pred):
    """Evaluation
	evaluate the predicted resul.

	# Arguments
		y_true: List/ndarray, ture data.
		y_pred: List/ndarray, predicted data.


	模型效果指标评估
	y_true:真实的数据值
	y_pred:回归模型预测的数据值
	explained_variance_score:解释回归模型的方差得分,其值取值范围是[0,1],越接近于1说明自变量越能解释因变量
	的方差变化,值越小则说明效果越差。
	mean_absolute_error:平均绝对误差(Mean Absolute Error,MAE),用于评估预测结果和真实数据集的接近程度的程度
	,其其值越小说明拟合效果越好。
	mean_squared_error:均方差(Mean squared error,MSE),该指标计算的是拟合数据和原始数据对应样本点的误差的
	平方和的均值,其值越小说明拟合效果越好。
	r2_score:判定系数,其含义是也是解释回归模型的方差得分,其值取值范围是[0,1],越接近于1说明自变量越能解释因
	变量的方差变化,值越小则说明效果越差。
	"""

    # mape = MAPE(y_true, y_pred)
    vs = metrics.explained_variance_score(y_true, y_pred)
    mae = metrics.mean_absolute_error(y_true, y_pred)
    mse = metrics.mean_squared_error(y_true, y_pred)
    r2 = metrics.r2_score(y_true, y_pred)
    print('explained_variance_score:%f' % vs, "   :larger is better")
    # print('mape:%f%%' % mape, "   :smaller is better")
    print('mae:%f' % mae, "   :smaller is better")
    print('mse:%f' % mse, "   :smaller is better")
    print('rmse:%f' % math.sqrt(mse), "   :smaller is better")
    print('r2:%f' % r2, "   : =1 is better")
def cgan_graph_simgan(settings, input_shape, simvae_model, gen_model, disc_model, tdw_img, latent_code_input, latent_noise_input):

    batchsize = settings.batchsize
    latent_code_dim = len(settings.latent_code_indices)
    latent_noise_dim = settings.latent_noise_dim

    #Encoder
    E_enc = simvae_model(latent_code_input, input_shape)

    #Decoder
    G_dec = gen_model(E_enc, latent_noise_input, input_shape)

    #Discriminator 
    DL,xin = disc_model(tdw_img, E_enc)
    GL,xin_gen = disc_model(G_dec, E_enc, reuse=True)


    #Disc Loss
    dr = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=DL,labels=tf.ones_like(DL)))
    df = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=GL,labels=tf.zeros_like(GL)))
    dloss = (dr + df)/2.

    gloss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=GL,labels=tf.ones_like(GL)))

    simloss = tf.reduce_mean(metrics.mean_squared_error(tdw_img, E_enc))

    return simloss, gloss, dloss, G_dec, E_enc
示例#4
0
 def call(self, inputs):
     x = inputs[0]
     y = inputs[1]
     self.add_loss(self.lmbda * K.mean(mean_squared_error(y, x)),
                   inputs=inputs)
     # We won't actually use the output.
     return x
示例#5
0
 def vae_loss(self, x, x_decoded_mean):
     #xent_loss = input_dim * metrics.binary_crossentropy(x, x_decoded_mean)
     #xent_loss = metrics.binary_crossentropy(x, x_decoded_mean)
     xent_loss = metrics.mean_squared_error(x, x_decoded_mean)
     kl_loss = self.beta * (-0.5 * K.sum(
         1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1))
     return K.mean(xent_loss + kl_loss)
示例#6
0
文件: mesh.py 项目: dpellow/MLPROJ
    def build_mesh(self):
        x = Input(shape=(self.original_dim, ))
        h = Dense(self.intermediate_dim, activation='relu')(x)
        z_mean = Dense(latent_dim)(h)
        z_log_var = Dense(latent_dim)(h)

        z = Lambda(self.sampling,
                   output_shape=(latent_dim, ))([z_mean, z_log_var])

        # we instantiate these layers separately so as to reuse them later
        decoder_h = Dense(self.intermediate_dim, activation='relu')
        decoder_mean = Dense(self.original_dim, activation='sigmoid')
        h_decoded = decoder_h(z)
        x_decoded_mean = decoder_mean(h_decoded)

        # instantiate VAE model
        self.vae = Model(x, x_decoded_mean)

        # Compute VAE loss
        vae_loss = self.original_dim * metrics.mean_squared_error(
            x, x_decoded_mean)

        xent_loss = self.original_dim * metrics.binary_crossentropy(
            x, x_decoded_mean
        )  # self.original_dim * metrics.mean_squared_error(x, x_decoded_mean)
        kl_loss = -0.5 * K.sum(
            1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1)
        vae_loss = xent_loss  # K.mean(xent_loss + kl_loss)

        self.vae.add_loss(vae_loss)
        self.vae.compile(optimizer='adam')  # rmsprop
        self.vae.summary()
def create_vae(batch_size, base_filters=64, latent=8,
               image_size=64, learning_rate=0.001,
               reconstruction_weight=1000, layers=4):
    '''
    Constructs VAE model with given parameters.
    :param batch_size: size of a batch (used for placeholder)
    :param base_filters: number of filters after first layer. Other layers will double this number
    :param latent: latent space dimension
    :param image_size: size of input image
    Returns compiled Keras model along with encoder and decoder
    '''
    if isinstance(image_size, int):
        image_size = (image_size, image_size)
    x = Input(batch_shape=(batch_size, image_size[0], image_size[1], 3))
    encoder = create_encoder([image_size[0], image_size[1], 3], base_filters=base_filters, latent=latent, layers=layers)
    decoder = create_decoder([image_size[0], image_size[1], 3], base_filters=base_filters, latent=latent, layers=layers)
    mean_log_var = encoder(x)
    mean_size = mean_log_var.shape[1]//2
    mean = Lambda(lambda h: h[:, :mean_size])(mean_log_var)
    log_var = Lambda(lambda h: h[:, mean_size:])(mean_log_var)
    z = Lambda(sample)([mean, log_var])
    reconstruction = decoder(z)
    loss_reconstruction = K.mean(metrics.mean_squared_error(x, reconstruction))
    loss_KL = - K.mean(0.5 * K.sum(1 + log_var - K.square(mean) - K.exp(log_var), axis=1))
    loss = reconstruction_weight*loss_reconstruction + loss_KL

    vae = Model(x, reconstruction)
    vae.compile(optimizer=keras.optimizers.Adam(lr=learning_rate), loss=lambda x, y: loss)
    return vae, encoder, decoder
示例#8
0
 def loss(x, x_decoded_mean):
     """ Calculate loss = reconstruction loss + KL loss for each data in minibatch """
     # E[log P(X^{a}|z)]
     recon_a = self.inpt_dim * metrics.mean_squared_error(x, x_decoded_mean)
     # D_KL(Q(z|X) || P(z|X)); calculate in closed form as both dist. are Gaussian
     kl_a = - 0.5 * K.sum(1 + z_vari - K.square(z_mean) - K.exp(z_vari), axis=-1)            
     fin_loss = recon_a + kl_a
     return fin_loss
示例#9
0
 def vae_loss(self, x, x_decoded_mean_squash):
     x = K.flatten(x)
     x_decoded_mean_squash = K.flatten(x_decoded_mean_squash)
     xent_loss = img_rows * img_cols * metrics.mean_squared_error(
         x, x_decoded_mean_squash)
     kl_loss = -0.5 * K.mean(
         1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1)
     return K.mean(xent_loss + kl_loss)
示例#10
0
            def ae_loss(self, p, x, charts_images):
                rec_loss = keras.layers.add([
                    p[:, i] / (2 * sigma**2) *
                    metrics.mean_squared_error(x, charts_images[i])
                    for i in range(k)
                ])

                return K.mean(rec_loss)  #- K.var(p)
 def vae_loss(self, x, x_decoded_mean):
     #xent_loss = original_dim * metrics.binary_crossentropy(x, x_decoded_mean)
     xent_loss = original_dim * metrics.mean_squared_error(
         x, x_decoded_mean)
     kl_loss = -0.5 * K.sum(
         1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1)
     #kl_loss_w = - 0.5 * K.sum(1 + w_log_var - K.square(w_mean) - K.exp(w_log_var), axis=-1)
     return K.mean(xent_loss + kl_loss)  # + kl_loss_w)
def rmse_nn(y_true, y_pred, scaling=1):
    """
    Function computes the RMSE.

    :param y_true: expected values
    :param y_pred: predicted values
    :return: computed RMSE
    """
    return K.sqrt(mean_squared_error(y_true/scaling, y_pred/scaling))
示例#13
0
def vae_loss(x, x_decoded_mean):
    x = K.batch_flatten(x)
    x_decoded_mean = K.batch_flatten(x_decoded_mean)
    xent_loss = original_dim * metrics.mean_squared_error(x, x_decoded_mean)
    #xent_loss = original_dim*metrics.mean_squared_error(x, x_decoded_mean)
    #xent_loss = K.sum(K.square(x_decoddded_mean-x), axis=-1)
    kl_loss = -0.5 * K.sum(1 + z_log_var - K.square(z_mean) - K.exp(z_log_var),
                           axis=-1)
    return xent_loss + BETA * kl_loss
示例#14
0
def get_encoder_loss(enc_loss_name):
    if enc_loss_name == 'mse_reconst':
        reconstr_loss = lambda x, y: metrics.mean_squared_error(x, y)
        loss_fn = lambda x, y: tf.reduce_mean(reconstr_loss(x, y))
    elif enc_loss_name == 'ce_reconst':
        reconstr_loss = lambda x, y: 64 * 64 * 1 * metrics.binary_crossentropy(
            x, y)
        loss_fn = lambda x, y: tf.reduce_mean(reconstr_loss(x, y))
    return loss_fn
示例#15
0
    def __init__(self, shape, action_shape, latent):
        super().__init__(shape, action_shape)

        # define the VAE layers
        input_img = Input(shape=shape)
        encoded = autoencoder.build_encoder(shape, 0.1, input_img)
        variational, mu, sigma = autoencoder.build_variational(latent, encoded)
        # decoded = autoencoder.MyAutoencoder.build_decoder(shape, 0.1, variational, latent)
        decoded = autoencoder.build_decoder(shape, 0.1, variational, latent)

        # define autoencoder loss
        # Compute VAE loss
        dec_loss = metrics.mean_squared_error(K.flatten(input_img),
                                              K.flatten(decoded))
        kl_loss = -0.5 * K.sum(1 + sigma - K.square(mu) - K.exp(sigma),
                               axis=-1)
        vae_loss = K.mean(dec_loss + kl_loss)

        # vae_loss = metrics.mean_squared_error(input_img, decoded)

        # define the ae model
        ae = Model(input_img, decoded)
        ae.add_loss(vae_loss)
        ae.summary()
        opt = Adam(lr=0.0001, clipvalue=0.5)
        # opt = SGD(lr=0.5, momentum=.9, clipvalue=0.5)
        ae.compile(optimizer=opt, loss=None,
                   metrics=['accuracy'])  # loss of None for compatibility
        self.ae = ae

        # define the model layers
        prev_action = Input((action_shape, ), name="Prev_Action")

        # define the control layers
        control_input = Concatenate()
        control = Dense(1024, activation='elu', name="Control")
        actions = Dense(action_shape, name="Actions")

        # define the full model
        full_input = control_input([prev_action, variational])
        full_control = control(full_input)
        full_actions = actions(full_control)
        full = Model([input_img, prev_action],
                     [decoded, variational, full_actions])
        full.summary()
        full.compile(opt, loss='mean_squared_error')
        self.full = full

        # define the controller training model
        latent_input = Input((latent, ))
        train_input = control_input([prev_action, latent_input])
        control_train = control(train_input)
        train_actions = actions(control_train)
        c_train = Model([prev_action, latent_input], train_actions)
        c_train.summary()
        c_train.compile(opt, loss='mean_squared_error')
        self.c_train = c_train
示例#16
0
 def vae_loss_gaussian(x, x_decoded_mean):
     # loss is averaged across features, mutiple # of features to scale back
     xent_loss = n_features * metrics.mean_squared_error(
         x, x_decoded_mean)
     kl_loss = -0.5 * K.sum(
         1 + z_log_var_encoded - K.square(z_mean_encoded) -
         K.exp(z_log_var_encoded),
         axis=-1)
     return K.mean(xent_loss + kl_loss)
示例#17
0
    def vae_loss(self, x, x_decoded_mean):
        """
        kl-divergence loss plus reconstrucion loss 
        """

        self.xent_loss = K.mean(metrics.mean_squared_error(self.target, x_decoded_mean), axis=1)  # sum error over time

        self.kl_loss = -0.5 * K.sum(1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1)
        return K.mean(self.xent_loss + self.kl_loss)
示例#18
0
def avg_batch_mse_loss_exaustive(y_true, y_pred):
    batch_size = K.int_shape(y_pred)[0]
    loss = 0
    N = 0
    for i in range(0, batch_size):
        for j in range(i + 1, batch_size):
            loss += mean_squared_error(y_pred[i], y_pred[j])
            N += 1
    loss /= N
示例#19
0
    def metric(y_true, y_pred):
        """
        Parameters
        ----------
        y_true : Keras tensor
            Keras tensor including the ground truth. Since the keras tensor includes an extra column to store the index of the data sample in the training set this column is ignored.
        y_pred : Keras tensor
            Keras tensor with the predictions of the contamination model (no data index).
        """

        return mean_squared_error(y_true[:, :nout], y_pred[:, :nout])
示例#20
0
def Model_variational_autoencoder(X):
    '''
    变分自编码器
    使用详情:https://github.com/MoyanZitto/keras-cn/blob/master/docs/legacy/blog/autoencoder.md 
    '''
    original_dim = X.shape[1]
    X_train, X_test = train_test_split(X,train_size=0.75, test_size=0.25,random_state=0)
    
    batch_size = 64
    latent_dim = 30
    intermediate_dim = 128
    epochs = 80
    epsilon_std = 1.0
    # 建立编码网络,将输入影射为隐分布的参数
    x = Input(shape=(original_dim,))
    h = Dense(intermediate_dim, activation='relu')(x)
    z_mean = Dense(latent_dim)(h)
    z_log_var = Dense(latent_dim)(h)
    # 从这些参数确定的分布中采样,这个样本相当于之前的隐层值
    def sampling(args):
        z_mean, z_log_var = args
        epsilon = K.random_normal(shape=(K.shape(z_mean)[0], latent_dim), mean=0.,
                              stddev=epsilon_std)
        return z_mean + K.exp(z_log_var / 2) * epsilon
    
    z = keras_Lambda(sampling, output_shape=(latent_dim,))([z_mean, z_log_var])
    # 采样得到的点映射回去重构原输入
    decoder_h = Dense(intermediate_dim, activation='relu')
    #因为输入的数据取值范围是0到1,所以用sigmoid可以很好的对应
    decoder_mean = Dense(original_dim, activation='sigmoid')
    h_decoded = decoder_h(z)
    x_decoded_mean = decoder_mean(h_decoded)
    # 构建VAE模型
    vae = Model(x, x_decoded_mean)
    # 使用端到端的模型训练,损失函数是一项重构误差,和一项KL距离
    #xent_loss = original_dim * keras_metrics.binary_crossentropy(x, x_decoded_mean)
    xent_loss = original_dim * keras_metrics.mean_squared_error(x, x_decoded_mean)
    kl_loss = - 0.5 * K.sum(1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1)
    vae_loss = K.mean(xent_loss + kl_loss)

    vae.add_loss(vae_loss)
    vae.compile(optimizer='rmsprop')
    vae.summary()
    
    vae.fit(X_train,
        shuffle=True,
        epochs=epochs,
        batch_size=batch_size,
        validation_data=(X_test, None))
    
    X_encoded = vae.predict(X)
    
    return X_encoded
示例#21
0
    def vae_loss(self, x, z_decoded):

        x = K.flatten(x)
        z_decoded = K.flatten(z_decoded)
        # 여기에서 - 가 곱해져 있다.
        cross_entropy_loss = metrics.mean_squared_error(x, z_decoded)

        kl_loss = 0.5 * K.mean(
            K.square(z_mean) + K.exp(z_log_var) - z_log_var - 1, axis=-1)
        #kl_loss = -5e-4 * K.mean(1 + z_log_var -
        #                         K.square(z_mean) - K.exp(z_log_var), axis=-1)

        return K.mean(cross_entropy_loss + kl_loss)
示例#22
0
    def vae_loss(self, x_input, x_decoded):
        if self.loss == 'binary_crossentropy':
            recon_loss = self.original_dim * \
                         metrics.binary_crossentropy(x_input, x_decoded)
        elif self.loss == 'mse':
            recon_loss = self.original_dim * \
                         metrics.mean_squared_error(x_input, x_decoded)

        kl_loss = -0.5 * K.sum(1 + self.var_layer - K.square(self.mean_layer) -
                               K.exp(self.var_layer),
                               axis=-1)

        return K.mean(recon_loss + (K.get_value(self.beta) * kl_loss))
def vae_loss(x, x_decoded_mean):
    #x_decoded_mean, x_decoded_log_std = x_dec
    #xent_loss = original_dim * metrics.binary_crossentropy(x, x_decoded_mean)
    #print x_decoded_mean
    #print x_decoded_log_std
    #term1 = K.square(x-x_decoded_mean)/(2.0 * K.exp(x_decoded_log_std))
    #term2 = 0.5 * K.sum(x_decoded_log_std)
    #reconstruction_loss = original_dim*(term1 + term2)
    reconstruction_loss = original_dim * metrics.mean_squared_error(
        x, x_decoded_mean)
    kl_loss = -0.5 * K.sum(1 + z_log_var - K.square(z_mean) - K.exp(z_log_var),
                           axis=-1)
    #kl_loss = - 0.5 * K.sum(1 + K.log(0.1+z_log_var) - K.square(z_mean) - z_log_var, axis=-1)
    #return xent_loss + kl_loss
    return reconstruction_loss + kl_loss
示例#24
0
    def metric(y_true, y_pred):
        """
        Parameters
        ----------
        y_true : Keras tensor
            Keras tensor including the ground truth
        y_pred : Keras tensor
            Keras tensor including the predictions of a heteroscedastic model. The predictions follow the order: (mean_0, S_0, mean_1, S_1, ...) with S_i the log of the variance for the ith output.
        """
        if nout > 1:
            y_out = K.reshape(y_pred[:, 0::nout], K.shape(y_true))
        else:
            y_out = K.reshape(y_pred[:, 0], K.shape(y_true))

        return mean_squared_error(y_true, y_out)
示例#25
0
def create_vae(batch_size,
               base_filters=64,
               latent=8,
               image_size=64,
               learning_rate=0.001,
               reconstruction_weight=1000,
               layers=4):
    """
    Constructs VAE model with given parameters.
    :param batch_size: size of a batch (used for placeholder)
    :param base_filters: number of filters after first layer.
        Other layers will double this number
    :param latent: latent space dimension
    :param image_size: size of input image
    Returns compiled Keras model along with encoder and decoder
    """
    if isinstance(image_size, int):
        image_size = (image_size, image_size)

    x = Input(batch_shape=(batch_size, image_size[0], image_size[1], 3))
    encoder = create_encoder([image_size[0], image_size[1], 3],
                             base_filters=base_filters,
                             latent=latent,
                             layers=layers)
    decoder = create_decoder([image_size[0], image_size[1], 3],
                             base_filters=base_filters,
                             latent=latent,
                             layers=layers)

    mean_log_var = encoder(x)
    mean_size = mean_log_var.shape[1] // 2

    mean = Lambda(lambda h: h[:, :mean_size])(mean_log_var)
    log_var = Lambda(lambda h: h[:, mean_size:])(mean_log_var)
    z = Lambda(sample)([mean, log_var])

    reconstruction = decoder(z)
    loss_reconstruction = K.mean(metrics.mean_squared_error(x, reconstruction))
    loss_KL = -K.mean(
        0.5 * K.sum(1 + log_var - K.square(mean) - K.exp(log_var), axis=1))
    loss = reconstruction_weight * loss_reconstruction + loss_KL

    vae = Model(x, reconstruction)
    vae.compile(optimizer=keras.optimizers.Adam(lr=learning_rate),
                loss=lambda x, y: loss)
    return vae, encoder, decoder
示例#26
0
    def vae_cos_loss(self, x, x_decoded_mean):


        #"""
        y_true = K.reshape(x,[-1,original_dim2,original_dim2])
        y_pred = K.reshape(x_decoded_mean,[-1,original_dim2,original_dim2])
        y_true = K.expand_dims(y_true,3)
        y_pred = K.expand_dims(y_pred,3)

        filtsz=5
        blurfilt = tf.ones([filtsz,filtsz])/(1.0*filtsz**2)
        blurfilt = tf.expand_dims(blurfilt,2)
        blurfilt = tf.expand_dims(blurfilt,3)

        y_true = tf.nn.convolution(y_true,blurfilt,'SAME')
        y_pred = tf.nn.convolution(y_pred,blurfilt,'SAME')
        y_true = K.reshape(y_true,[-1,original_dim])
        y_pred = K.reshape(y_pred,[-1,original_dim])
        print('y true',y_true)
        #"""
        #xent_loss = original_dim * metrics.binary_crossentropy(x, x_decoded_mean)
        #xent_loss = original_dim * metrics.mean_squared_error(x, x_decoded_mean)
        xent_loss = original_dim * metrics.mean_squared_error(y_true, y_pred)

        #xent_loss = original_dim * res
        """

        """

        kl_loss = - 0.5 * K.sum(1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1)
        kl_loss_w = - 0.5 * K.sum(1 + w_log_var - K.square(w_mean) - K.exp(w_log_var), axis=-1)
        #kl_loss_w = - 0.5 * K.sum(1 + w_log_var - K.square(w_mean) - K.exp(w_log_var), axis=-1)
        #kl_loss = tf.Print(kl_loss,
        #                   [K.get_variable_shape(xent_loss)],
        #                   'xent ssim ')
        return K.mean(xent_loss + kl_loss + kl_loss_w)
示例#27
0
def value_bellman_mean_squared_error(bellman_targets, value_predictions):
    return metrics.mean_squared_error(bellman_targets, value_predictions)
class ConvexOptimiser(object):
    def __init__(self, cost_function, tensor_shape):
        self.cost_function = cost_function
        self.tensor_shape = tensor_shape
        self.gradient_values = None

    def loss(self, point):
        loss_value, self.gradient_values = self.cost_function(
            [point.reshape(self.tensor_shape)])
        return loss_value.astype(np.float64)

    def gradients(self, point):
        return self.gradient_values.flatten().astype(np.float64)


mse_loss = metrics.mean_squared_error(content_layer, target)
grads = K.gradients(mse_loss, vgg_model.input)
cost_function = K.function([vgg_model.input], [mse_loss] + grads)
optimiser = ConvexOptimiser(cost_function, content_image_shape)


def optimise(optimiser, iterations, point, tensor_shape, file_name):
    for i in range(iterations):
        point, min_val, info = fmin_l_bfgs_b(optimiser.loss,
                                             point.flatten(),
                                             fprime=optimiser.gradients,
                                             maxfun=20)
        point = np.clip(point, -127, 127)
        print('Loss:', min_val)
        imsave(work_dir + 'gen_' + file_name + '_{i}.png',
               add_imagenet_mean(point.copy(), tensor_shape)[0])
                        mode='auto',
                        restore_best_weights=True)
model.fit(x_train,
          y_train,
          validation_data=(x_test, y_test),
          callbacks=[monitor],
          verbose=2,
          epochs=1000)

from sklearn import metrics

# Predict
pred = model.predict(x_test)

# Measure MSE error.
score = metrics.mean_squared_error(pred, y_test)
print("Final score (MSE): {}".format(score))

import numpy as np

# Measure RMSE error.  RMSE is common for regression.
score = np.sqrt(metrics.mean_squared_error(pred, y_test))
print("Final score (RMSE): {}".format(score))


def chart_regression(pred, y, sort=True):
    t = pd.DataFrame({'pred': pred, 'y': y.flatten()})
    if sort:
        t.sort_values(by=['y'], inplace=True)
    plt.plot(t['y'].tolist(), label='expected')
    plt.plot(t['pred'].tolist(), label='prediction')
示例#30
0
def mse_heteroscedastic(y_true, y_pred):
    y_out = K.reshape(y_pred[:,:-1], K.shape(y_true))
    return mean_squared_error(y_true, y_out)
# note that "output_shape" isn't necessary with the TensorFlow backend
z = Lambda(sampling, output_shape=(latent_dim, ))([z_mean, z_log_var])

# we instantiate these layers separately so as to reuse them later
decoder_h = Dense(intermediate_dim, activation='tanh')
decoder_mean = Dense(original_dim, activation='tanh')
h_decoded = decoder_h(z)
x_decoded_mean = decoder_mean(h_decoded)

# instantiate VAE model
vae = Model(x, x_decoded_mean)

# Compute VAE loss
#binary_crossentropy
xent_loss = original_dim * metrics.mean_squared_error(x, x_decoded_mean)
kl_loss = -0.5 * K.sum(1 + z_log_var - K.square(z_mean) - K.exp(z_log_var),
                       axis=-1)
vae_loss = K.mean(xent_loss + kl_loss)

vae.add_loss(vae_loss)
vae.compile(optimizer='rmsprop')
vae.summary()

# train the VAE on MNIST digits
x_train = df2  #.iloc# [0:1000,:]
x_test = df2  #.iloc#[1000:,:]

strats = [
    '', 'EMAXover', 'Ichimoku', 'MACD', 'MovAveXover', 'ACC', 'Boll',
    'Fast Stochastic', 'MARSI', 'AntiBCRP', 'BCRP', 'PROC', 'RSI', 'SAR',
class ConvexOptimiser(object):
    def __init__(self, cost_function, tensor_shape):
        self.cost_function = cost_function
        self.tensor_shape = tensor_shape
        self.gradient_values = None

    def loss(self, point):
        loss_value, self.gradient_values = self.cost_function([point.reshape(self.tensor_shape)])
        return loss_value.astype(np.float64)

    def gradients(self, point):
        return self.gradient_values.flatten().astype(np.float64)


mse_loss = metrics.mean_squared_error(content_layer, target)
grads = K.gradients(mse_loss, vgg_model.input)
cost_function = K.function([vgg_model.input], [mse_loss]+grads)
optimiser = ConvexOptimiser(cost_function, content_image_shape)


def optimise(optimiser, iterations, point, tensor_shape, file_name):
    for i in range(iterations):
        point, min_val, info = fmin_l_bfgs_b(optimiser.loss, point.flatten(),
                                         fprime=optimiser.gradients, maxfun=20)
        point = np.clip(point, -127, 127)
        print('Loss:', min_val)
        imsave(work_dir + 'gen_'+file_name+'_{i}.png', add_imagenet_mean(point.copy(), tensor_shape)[0])
    return point