标签: keras-layer

在Keras中的TimeDistributed(Dense)vs Dense - 相同数量的参数

我正在构建一个模型,使用循环图层(GRU)将字符串转换为另一个字符串.我已经尝试了Dense和TimeDistributed(密集)层作为最后一层,但我不明白使用return_sequences = True时两者之间的区别,特别是因为它们似乎具有相同数量的参数.

我的简化模型如下:

InputSize = 15
MaxLen = 64
HiddenSize = 16

inputs = keras.layers.Input(shape=(MaxLen, InputSize))
x = keras.layers.recurrent.GRU(HiddenSize, return_sequences=True)(inputs)
x = keras.layers.TimeDistributed(keras.layers.Dense(InputSize))(x)
predictions = keras.layers.Activation('softmax')(x)
Run Code Online (Sandbox Code Playgroud)

网络摘要是:

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         (None, 64, 15)            0         
_________________________________________________________________
gru_1 (GRU)                  (None, 64, 16)            1536      
_________________________________________________________________
time_distributed_1 (TimeDist (None, 64, 15)            255       
_________________________________________________________________
activation_1 (Activation)    (None, 64, 15)            0         
=================================================================
Run Code Online (Sandbox Code Playgroud)

这对我来说很有意义,因为我对TimeDistributed的理解是它在所有时间点都应用相同的层,因此Dense层有16*15 + 15 = 255个参数(权重+偏差).

但是,如果我切换到一个简单的Dense图层:

inputs = keras.layers.Input(shape=(MaxLen, InputSize))
x = keras.layers.recurrent.GRU(HiddenSize, return_sequences=True)(inputs)
x = …
Run Code Online (Sandbox Code Playgroud)

machine-learning neural-network keras recurrent-neural-network keras-layer

22
推荐指数
1
解决办法
5994
查看次数

Keras 2.x - 获取图层权重

我使用的是Windows 10,Python 3.5和tensorflow 1.1.0.我有以下脚本:

import tensorflow as tf
import tensorflow.contrib.keras.api.keras.backend as K
from tensorflow.contrib.keras.api.keras.layers import Dense

tf.reset_default_graph()
init = tf.global_variables_initializer()
sess =  tf.Session()
K.set_session(sess) # Keras will use this sesssion to initialize all variables

input_x = tf.placeholder(tf.float32, [None, 10], name='input_x')    
dense1 = Dense(10, activation='relu')(input_x)

sess.run(init)

dense1.get_weights()
Run Code Online (Sandbox Code Playgroud)

我收到错误: AttributeError: 'Tensor' object has no attribute 'weights'

我做错了什么,我怎么得到权重dense1?我看过这个这个 SO帖子,但我仍然无法使它工作.

python deep-learning keras tensorflow keras-layer

21
推荐指数
4
解决办法
3万
查看次数

嵌入层和密集层之间有什么区别?

Keras 嵌入层的文档说:

将正整数(索引)转换为固定大小的密集向量.例如.[[4], [20]]- >[[0.25, 0.1], [0.6, -0.2]]

我相信这也可以通过将输入编码为长度的一个热矢量vocabulary_size并将它们馈送到密集层来实现.

嵌入层只是这个两步过程的便利,还是引人入胜的东西?

machine-learning neural-network deep-learning keras keras-layer

20
推荐指数
2
解决办法
4508
查看次数

如何使用TFRecord数据集快速制作TensorFlow + Keras?

什么是如何将TensorFlow TFRecord与Keras模型和tf.session.run()一起使用,同时将数据集保存在具有队列运行程序的张量中?

以下是一个可行的代码段,但需要进行以下改进:

  • 使用Model API
  • 指定一个Input()
  • 从TFRecord加载数据集
  • 并行运行数据集(例如使用queuerunner)

这是片段,有几条TODO线表明需要什么:

from keras.models import Model
import tensorflow as tf
from keras import backend as K
from keras.layers import Dense, Input
from keras.objectives import categorical_crossentropy
from tensorflow.examples.tutorials.mnist import input_data

sess = tf.Session()
K.set_session(sess)

# Can this be done more efficiently than placeholders w/ TFRecords?
img = tf.placeholder(tf.float32, shape=(None, 784))
labels = tf.placeholder(tf.float32, shape=(None, 10))

# TODO: Use Input() 
x = Dense(128, activation='relu')(img)
x = Dense(128, activation='relu')(x)
preds = Dense(10, activation='softmax')(x)
# TODO: …
Run Code Online (Sandbox Code Playgroud)

machine-learning deep-learning keras tensorflow keras-layer

19
推荐指数
2
解决办法
1万
查看次数

ImportError:无法从keras导入名称'_obtain_input_shape'

在Keras,

我正在尝试导入_obtain_input_shape如下:

from keras.applications.imagenet_utils import _obtain_input_shape
Run Code Online (Sandbox Code Playgroud)

但是,我收到以下错误:

ImportError:无法导入名称'_obtain_input_shape'

我试图导入_obtain_input_shape的原因是我可以确定输入形状(以便按如下方式加载VGG-Face:

我用它来确定输入张量的正确输入形状如下:

input_shape = _obtain_input_shape(input_shape,
                                  default_size=224,
                                  min_size=48,
                                  data_format=K.image_data_format(),
                                  require_flatten=include_top)`
Run Code Online (Sandbox Code Playgroud)

请协助?提前致谢.

keras keras-layer keras-2

19
推荐指数
3
解决办法
2万
查看次数

Tensorflow分配内存:38535168的分配超过系统内存的10%

使用ResNet50预训练的权重我正在尝试构建一个分类器.代码库完全在Keras高级Tensorflow API中实现.完整的代码发布在下面的GitHub链接中.

源代码:使用RestNet50架构进行分类

预训练模型的文件大小为94.7mb.

我加载了预先训练好的文件

new_model = Sequential()

new_model.add(ResNet50(include_top=False,
                pooling='avg',
                weights=resnet_weight_paths))
Run Code Online (Sandbox Code Playgroud)

并适合模型

train_generator = data_generator.flow_from_directory(
    'path_to_the_training_set',
    target_size = (IMG_SIZE,IMG_SIZE),
    batch_size = 12,
    class_mode = 'categorical'
    )

validation_generator = data_generator.flow_from_directory(
    'path_to_the_validation_set',
    target_size = (IMG_SIZE,IMG_SIZE),
    class_mode = 'categorical'
    )

#compile the model

new_model.fit_generator(
    train_generator,
    steps_per_epoch = 3,
    validation_data = validation_generator,
    validation_steps = 1
)
Run Code Online (Sandbox Code Playgroud)

在训练数据集中,我有两个文件夹狗和猫,每个持有近10,000张图像.当我编译脚本时,我收到以下错误

Epoch 1/1 2018-05-12 13:04:45.847298:W tensorflow/core/framework/allocator.cc:101] 38535168的分配超过系统内存的10%.2018-05-12 13:04:46.845021:W tensorflow/core/framework/allocator.cc:101] 37171200的分配超过系统内存的10%.2018-05-12 13:04:47.552176:W tensorflow/core/framework/allocator.cc:101] 37171200的分配超过系统内存的10%.2018-05-12 13:04:48.199240:W tensorflow/core/framework/allocator.cc:101] 37171200的分配超过系统内存的10%.2018-05-12 13:04:48.918930:W tensorflow/core/framework/allocator.cc:101] 37171200的分配超过系统内存的10%.2018-05-12 13:04:49.274137:W tensorflow/core/framework/allocator.cc:101] 19267584的分配超过系统内存的10%.2018-05-12 13:04:49.647061:W tensorflow/core/framework/allocator.cc:101] 19267584的分配超过系统内存的10%.2018-05-12 …

python memory tensorflow resnet keras-layer

19
推荐指数
5
解决办法
3万
查看次数

调整Keras Lambda图层中的输入图像大小

我希望我的keras模型使用cv2或类似的方法调整输入图像的大小.

我已经看过了使用ImageGenerator,但我更喜欢编写自己的生成器,只需调整第一层图像的大小keras.layers.core.Lambda.

我该怎么做?

python keras keras-layer cv2

18
推荐指数
1
解决办法
1万
查看次数

Keras错误"您必须使用dtype bool为占位符张量'bidirectional_1/keras_learning_phase'提供值"

我在下面的代码段中收到以下错误:

您必须使用dtype bool为占位符张量'bidirectional_1/keras_learning_phase'提供值

如果我添加了dropout图层model.add(Dropout(dropout)),它就可以了.谁知道为什么?后端是Tensorflow,Keras 2.0.1

def prep_model1(embedding_layer1, embedding_layer2, dropout=0.5):

    model0 = Sequential()  
    model0.add(embedding_layer1)
    model0.add(Bidirectional(LSTM(128, return_sequences=False, dropout=dropout)))

    model1 = Sequential() 
    model1.add(embedding_layer2)
    model1.add(Bidirectional(LSTM(128, return_sequences=False, dropout=dropout)))

    model = Sequential()
    model.add(Merge([model0, model1], mode='concat', concat_axis=1))
    #model.add(Dropout(dropout))
    model.add(Dense(1, activation='sigmoid'))

    return model
Run Code Online (Sandbox Code Playgroud)

deep-learning keras tensorflow keras-layer

18
推荐指数
1
解决办法
1万
查看次数

Keras详细培训进度条在每个批次问题上写一个新行

在Keras运行密集的前馈神经网络.两个输出有class_weights,第三个输出有sample_weights.出于某种原因,它打印出计算的每个批次的进度详细显示,而不是更新与其应该在同一行上的打印...这是否曾经发生在你身上?它是如何修复的?从shell:

42336/747322 [====>.........................] - ETA: 79s - loss: 20.7154 - x1_loss: 9.5913 - x2_loss: 10.0536 - x3_loss: 1.0705 - x1_acc: 0.6930 - x2_acc: 0.4433 - x3_acc: 0.6821
143360/747322 [====>.........................] - ETA: 78s - loss: 20.7387 - x1_loss: 9.6131 - x2_loss: 10.0555 - x3_loss: 1.0702 - x1_acc: 0.6930 - x2_acc: 0.4432 - x3_acc: 0.6820
144384/747322 [====>.........................] - ETA: 78s - loss: 20.7362 - x1_loss: 9.6067 - x2_loss: 10.0608 - x3_loss: 1.0687 - x1_acc: 0.6930 - x2_acc: 0.4429 - x3_acc: 0.6817
145408/747322 …
Run Code Online (Sandbox Code Playgroud)

progress-bar theano keras tensorflow keras-layer

16
推荐指数
4
解决办法
4775
查看次数

Keras中Conv2D与Convolution2D的区别

Tensorflow已经有了答案.但问题是在我的IDE中Conv2D是一个类,而Convolution2D是一个变量?

machine-learning convolution neural-network keras keras-layer

16
推荐指数
1
解决办法
6574
查看次数