当我同时使用tensorflow和keras时错了吗?

时间:2019-05-20 06:03:07

标签: tensorflow keras

这是我将tensorflow与keras混合在一起的代码。

def dense_block(x, nb_layers, nb_filter, growth_rate, bottleneck=False, dropout_rate=None, weight_decay=1e-4,
                  grow_nb_filters=True, return_concat_list=False):
    ''' Build a dense_block where the output of each conv_block is fed to subsequent ones
    Args:
        x: keras tensor
        nb_layers: the number of layers of conv_block to append to the model.
        nb_filter: number of filters
        growth_rate: growth rate
        bottleneck: bottleneck block
        dropout_rate: dropout rate
        weight_decay: weight decay factor
        grow_nb_filters: flag to decide to allow number of filters to grow
        return_concat_list: return the list of feature maps along with the actual output
    Returns: keras tensor with nb_layers of conv_block appended
    '''
    concat_axis = 1 if K.image_data_format() == 'channels_first' else -1
    x_list = [x]
    for i in range(nb_layers):
        cb = __conv_block(x, growth_rate, bottleneck, dropout_rate, weight_decay)
        ########################################################
        #This is where I mix tensorflow with keras.
        cb = K.reshape(cb,(-1,7*7*32))
        W = weight_variable([7*7*32,7*7*32])
        cb = tf.matmul(cb, W)
        cb = K.reshape(cb,(-1,7,7,32))
        x_list.append(cb)
        ######################################################
        x = concatenate([x, cb], axis=concat_axis)

        if grow_nb_filters:
            nb_filter += growth_rate
    if return_concat_list:
        return x, nb_filter, x_list
    else:
        return x, nb_filter

,但出现这样的错误:

AttributeError:“张量”对象没有属性“ _keras_history”

1 个答案:

答案 0 :(得分:0)

通常无法使用Tensoflow中提供的安装程序将单独的keras安装与tensorflow混合使用

尝试按以下方式替换keras from tensorflow import keras as K

https://www.tensorflow.org/api_docs/python/tf/keras