将Keras转换为Tensorflow-修剪+概率

时间:2019-05-20 15:56:17

标签: python tensorflow keras keras-layer tf.keras

我有一个代码,将Tensorflow概率(需要TF 2.00)与Keras修剪混合,修剪了第一个密集层的权重并提供TF概率的输入,这两个代码(Keras + TF)在同一模型中。代码:

.sub(/[\?&]gclid=[^&]*/, '')

错误的详细信息:

from tensorflow_model_optimization.sparsity import keras as sparsity
from tensorflow.python import keras
import numpy as np
tf.disable_v2_behavior()

epochs = 50
num_train_samples = x1.shape[0]
end_step = 500
print('End step: ' + str(end_step))

tfd = tfp.distributions

input_shape=x1.shape

init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    pruned_model = tf.keras.Sequential([
        sparsity.prune_low_magnitude(
            tf.keras.layers.Dense(1, activation='relu'),**pruning_params),
      tfp.layers.DistributionLambda(lambda t: tfd.Normal(loc=t, scale=1))
    ])

    negloglik = lambda x, rv_x: -rv_x.log_prob(x)


    pruned_model.compile(optimizer=tf.keras.optimizers.Adam(lr=0.0001), loss=negloglik)

    callbacks = [
        pruning_callbacks.UpdatePruningStep(),
        pruning_callbacks.PruningSummaries(log_dir="D:\Python\logs2", profile_batch=0)]

    # ERROR HERE IN .fit()

    pruned_model.fit(x1,y, epochs=50, verbose=True, batch_size=16,callbacks=callbacks)

    yhat2 = pruned_model(np.array(dataframe.iloc[:,1]).T.astype(np.float32).reshape(-1,1)[650:800])
    mean02 = tf.convert_to_tensor(yhat2)
    mean2 = sess.run(mean02)    
    stddev2 = yhat2.stddev()
    mean_plus_2_std2 = sess.run(mean2 - 3. * stddev2)
    mean_minus_2_std2 = sess.run(mean2 + 3. * stddev2)

我的问题是:如何将Keras层(prune_low_magnitude)转换为Tensorflow,或如何将Tensorflow概率层(tfp.layers.DistributionLambda)转换为Keras并正确训练模型?

笔记本使用Keras == 2.2.4和Tensorflow == 2.0.0a0

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。我安装了:

! pip install --upgrade tfp-nightly
! pip install tf_nightly
! pip install tf_estimator_nightly