为您推荐:
精华内容
最热下载
问答
  • 5星
    9.55MB HUANGliang_ 2021-08-13 14:52:28
  • 5星
    1.18MB weixin_45771864 2021-07-19 16:11:07
  • 5星
    4KB weixin_42682925 2021-10-03 02:37:31
  • 5星
    8.11MB weixin_45087775 2021-04-22 10:36:29
  • 5星
    88KB weixin_54707168 2021-05-05 09:51:24
  • 5星
    93.95MB qq_21548021 2021-06-28 10:17:26
  • 5星
    5.3MB m0_52384018 2021-07-01 01:59:37
  • 5星
    406.57MB qq_42324086 2021-08-05 16:13:28
  • 5星
    8.8MB weixin_47723732 2021-04-19 11:45:51
  • 5星
    111.52MB qq_28239511 2021-03-17 13:04:53
  • 本篇详细说明merge的应用,join 和concatenate的拼接方法的与之相似。pd.merge(left, right, how='inner', on=None, left_on=None, right_on=None,left_index=False, right_index=False, sort=True,suffixes=('_x', ...

    本篇详细说明merge的应用,join 和concatenate的拼接方法的与之相似。

    pd.merge(left, right, how='inner', on=None, left_on=None, right_on=None,

    left_index=False, right_index=False, sort=True,

    suffixes=('_x', '_y'), copy=True, indicator=False,

    validate=None)

    1

    2

    3

    4

    参数如下:

    left: 拼接的左侧DataFrame对象

    right: 拼接的右侧DataFrame对象

    on: 要加入的列或索引级别名称。 必须在左侧和右侧DataFrame对象中找到。 如果未传递且left_index和right_index为False,则DataFrame中的列的交集将被推断为连接键。

    left_on:左侧DataFrame中的列或索引级别用作键。 可以是列名,索引级名称,也可以是长度等于DataFrame长度的数组。

    right_on: 左侧DataFrame中的列或索引级别用作键。 可以是列名,索引级名称,也可以是长度等于DataFrame长度的数组。

    left_index: 如果为True,则使用左侧DataFrame中的索引(行标签)作为其连接键。 对于具有MultiIndex(分层)的DataFrame,级别数必须与右侧DataFrame中的连接键数相匹配。

    right_index: 与left_index功能相似。

    how: One of ‘left’, ‘right’, ‘outer’, ‘inner’. 默认inner。inner是取交集,outer取并集。比如left:[‘A’,‘B’,‘C’];right[’'A,‘C’,‘D’];inner取交集的话,left中出现的A会和right中出现的买一个A进行匹配拼接,如果没有是B,在right中没有匹配到,则会丢失。'outer’取并集,出现的A会进行一一匹配,没有同时出现的会将缺失的部分添加缺失值。

    sort: 按字典顺序通过连接键对结果DataFrame进行排序。 默认为True,设置为False将在很多情况下显着提高性能。

    suffixes: 用于重叠列的字符串后缀元组。 默认为(‘

    x’,’

    y’)。

    copy: 始终从传递的DataFrame对象复制数据(默认为True),即使不需要重建索引也是如此。

    indicator:将一列添加到名为_merge的输出DataFrame,其中包含有关每行源的信息。 _merge是分类类型,并且对于其合并键仅出现在“左”DataFrame中的观察值,取得值为left_only,对于其合并键仅出现在“右”DataFrame中的观察值为right_only,并且如果在两者中都找到观察点的合并键,则为left_only。

    1、基础实例:

    import pandas as pd

    left = pd.DataFrame({'key': ['K0', 'K1', 'K2', 'K3'],

    'A': ['A0', 'A1', 'A2', 'A3'],

    'B': ['B0', 'B1', 'B2', 'B3']})

    right = pd.DataFrame({'key': ['K0', 'K1', 'K2', 'K3'],

    'C': ['C0', 'C1', 'C2', 'C3'],

    'D': ['D0', 'D1', 'D2', 'D3']})

    result = pd.merge(left, right, on='key')

    # on参数传递的key作为连接键

    result

    Out[4]:

    A B key C D

    0 A0 B0 K0 C0 D0

    1 A1 B1 K1 C1 D1

    2 A2 B2 K2 C2 D2

    3 A3 B3 K3 C3 D3

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    2、传入的on的参数是列表:

    left = pd.DataFrame({'key1': ['K0', 'K0', 'K1', 'K2'],

    'key2': ['K0', 'K1', 'K0', 'K1'],

    'A': ['A0', 'A1', 'A2', 'A3'],

    'B': ['B0', 'B1', 'B2', 'B3']})

    right = pd.DataFrame({'key1': ['K0', 'K1', 'K1', 'K2'],

    'key2': ['K0', 'K0', 'K0', 'K0'],

    'C': ['C0', 'C1', 'C2', 'C3'],

    'D': ['D0', 'D1', 'D2', 'D3']})

    result = pd.merge(left, right, on=['key1', 'key2'])

    # 同时传入两个Key,此时会进行以['key1','key2']列表的形式进行对应,left的keys列表是:[['K0', 'K0'],['K0', 'K1'],['K1', 'K0'],['K2', 'K1']],

    left的keys列表是:[['K0', 'K0'],['K1', 'K0'],['K1', 'K0'],['K2', 'K0']],因此会有1个['K0', 'K0']、2个['K1', 'K0']对应。

    result

    Out[6]:

    A B key1 key2 C D

    0 A0 B0 K0 K0 C0 D0

    1 A2 B2 K1 K0 C1 D1

    2 A2 B2 K1 K0 C2 D2

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    3、Merge method

    如果组合键没有出现在左表或右表中,则连接表中的值将为NA。

    Merge method

    SQL Join Name

    Description

    left

    LEFTOUTER JOIN

    Use keys from left frame only

    right

    RIGHT OUTER JOIN

    Use keys from right frame only

    outer

    FULL OUTER JOIN

    Use union of keys from both frames

    inner

    INNER JOIN

    Use intersection of keys from both frames

    result = pd.merge(left, right, how='left', on=['key1', 'key2'])

    # Use keys from left frame only

    result

    Out[34]:

    A B key1 key2 C D

    0 A0 B0 K0 K0 C0 D0

    1 A1 B1 K0 K1 NaN NaN

    2 A2 B2 K1 K0 C1 D1

    3 A2 B2 K1 K0 C2 D2

    4 A3 B3 K2 K1 NaN NaN

    result = pd.merge(left, right, how='right', on=['key1', 'key2'])

    # Use keys from right frame only

    result

    Out[36]:

    A B key1 key2 C D

    0 A0 B0 K0 K0 C0 D0

    1 A2 B2 K1 K0 C1 D1

    2 A2 B2 K1 K0 C2 D2

    3 NaN NaN K2 K0 C3 D3

    result = pd.merge(left, right, how='outer', on=['key1', 'key2'])

    # Use intersection of keys from both frames

    result

    Out[38]:

    A B key1 key2 C D

    0 A0 B0 K0 K0 C0 D0

    1 A1 B1 K0 K1 NaN NaN

    2 A2 B2 K1 K0 C1 D1

    3 A2 B2 K1 K0 C2 D2

    4 A3 B3 K2 K1 NaN NaN

    5 NaN NaN K2 K0 C3 D3

    -----------------------------------------------------

    left = pd.DataFrame({'A' : [1,2], 'B' : [2, 2]})

    right = pd.DataFrame({'A' : [4,5,6], 'B': [2,2,2]})

    result = pd.merge(left, right, on='B', how='outer')

    result

    Out[40]:

    A_x B A_y

    0 1 2 4

    1 1 2 5

    2 1 2 6

    3 2 2 4

    4 2 2 5

    5 2 2 6

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    41

    42

    43

    44

    45

    4、传入indicator参数

    merge接受参数指示符。 如果为True,则将名为_merge的Categorical类型列添加到具有值的输出对象:

    Observation Origin

    _merge value

    Merge key only in ‘left’ frame

    left_only

    Merge key only in ‘right’ frame

    right_only

    Merge key in

    both frames

    df1 = pd.DataFrame({'col1': [0, 1], 'col_left':['a', 'b']})

    df2 = pd.DataFrame({'col1': [1, 2, 2],'col_right':[2, 2, 2]})

    pd.merge(df1, df2, on='col1', how='outer', indicator=True)

    Out[44]:

    col1 col_left col_right _merge

    0 0.0 a NaN left_only

    1 1.0 b 2.0 both

    2 2.0 NaN 2.0 right_only

    3 2.0 NaN 2.0 right_only

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    指标参数也将接受字符串参数,在这种情况下,指标函数将使用传递的字符串的值作为指标列的名称。

    pd.merge(df1, df2, on='col1', how='outer', indicator='indicator_column')

    Out[45]:

    col1 col_left col_right indicator_column

    0 0.0 a NaN left_only

    1 1.0 b 2.0 both

    2 2.0 NaN 2.0 right_only

    3 2.0 NaN 2.0 right_only

    1

    2

    3

    4

    5

    6

    7

    5、以index为链接键

    需要同时设置left_index= True 和 right_index= True,或者left_index设置的同时,right_on指定某个Key。总的来说就是需要指定left、right链接的键,可以同时是key、index或者混合使用。

    left = pd.DataFrame({'A': ['A0', 'A1', 'A2'],

    ....: 'B': ['B0', 'B1', 'B2']},

    ....: index=['K0', 'K1', 'K2'])

    ....:

    right = pd.DataFrame({'C': ['C0', 'C2', 'C3'],

    ....: 'D': ['D0', 'D2', 'D3']},

    ....: index=['K0', 'K2', 'K3'])

    ....:

    # 只有K0、K2有对应的值

    pd.merge(left,right,how= 'inner',left_index=True,right_index=True)

    Out[51]:

    A B C D

    K0 A0 B0 C0 D0

    K2 A2 B2 C2 D2

    left = pd.DataFrame({'A': ['A0', 'A1', 'A2', 'A3'],

    'B': ['B0', 'B1', 'B2', 'B3'],

    'key': ['K0', 'K1', 'K0', 'K1']})

    right = pd.DataFrame({'C': ['C0', 'C1'],

    'D': ['D0', 'D1']},

    index=['K0', 'K1'])

    result = pd.merge(left, right, left_on='key', right_index=True, how='left', sort=False)

    # left_on='key', right_index=True

    result

    Out[54]:

    A B key C D

    0 A0 B0 K0 C0 D0

    1 A1 B1 K1 C1 D1

    2 A2 B2 K0 C0 D0

    3 A3 B3 K1 C1 D1

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    6、sort对链接的键值进行排序:

    紧接着上一例,设置sort= True

    result = pd.merge(left, right, left_on='key', right_index=True, how='left', sort=True)

    result

    Out[57]:

    A B key C D

    0 A0 B0 K0 C0 D0

    2 A2 B2 K0 C0 D0

    1 A1 B1 K1 C1 D1

    3 A3 B3 K1 C1 D1

    展开全文
    weixin_39946657 2021-01-28 21:56:23
  • 本文整理汇总了Python中keras.layers.merge.Add方法的典型用法代码示例。如果您正苦于以下问题:Python merge.Add方法的具体用法?Python merge.Add怎么用?Python merge.Add使用的例子?那么恭喜您, 这里精选的方法...

    本文整理汇总了Python中keras.layers.merge.Add方法的典型用法代码示例。如果您正苦于以下问题:Python merge.Add方法的具体用法?Python merge.Add怎么用?Python merge.Add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在模块keras.layers.merge的用法示例。

    在下文中一共展示了merge.Add方法的28个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

    示例1: _build_residual_block

    ​点赞 6

    # 需要导入模块: from keras.layers import merge [as 别名]

    # 或者: from keras.layers.merge import Add [as 别名]

    def _build_residual_block(args, x):

    cnn_filter_num = args['cnn_filter_num']

    cnn_filter_size = args['cnn_filter_size']

    l2_reg = args['l2_reg']

    in_x = x

    x = Conv2D(filters=cnn_filter_num, kernel_size=cnn_filter_size, padding="same",

    data_format="channels_first", kernel_regularizer=l2(l2_reg))(x)

    x = BatchNormalization(axis=1)(x)

    x = Activation("relu")(x)

    x = Conv2D(filters=cnn_filter_num, kernel_size=cnn_filter_size, padding="same",

    data_format="channels_first", kernel_regularizer=l2(l2_reg))(x)

    x = BatchNormalization(axis=1)(x)

    x = Add()([in_x, x])

    x = Activation("relu")(x)

    return x

    开发者ID:witchu,项目名称:alphazero,代码行数:18,

    示例2: add_conv_layer

    ​点赞 6

    # 需要导入模块: from keras.layers import merge [as 别名]

    # 或者: from keras.layers.merge import Add [as 别名]

    def add_conv_layer(input_list, layer_name, nb_filters, kernel_size, padding, dropout_rate=0.1,

    activation='relu', strides=1, attention_level=0, conv_option="normal", prev_conv_tensors=None):

    conv_layer = Convolution1D(filters=nb_filters, kernel_size=kernel_size, padding=padding,

    activation=activation, strides=strides, name=layer_name)

    max_pooling_layer = GlobalMaxPooling1D()

    dropout_layer = Dropout(dropout_rate)

    output_list, conv_output_list = [], []

    for i in range(len(input_list)):

    input = input_list[i]

    conv_tensor = conv_layer(input)

    if conv_option == "ResNet":

    conv_tensor = Add()([conv_tensor, prev_conv_tensors[i][-1]])

    dropout_tensor = dropout_layer(conv_tensor)

    #conv_pooling_tensor = max_pooling_layer(conv_tensor)

    output_list.append(dropout_tensor)

    #conv_output_list.append(conv_pooling_tensor)

    conv_output_list.append(conv_tensor)

    return output_list, conv_output_list

    开发者ID:jinfengr,项目名称:neural-tweet-search,代码行数:20,

    示例3: conv_block

    ​点赞 6

    # 需要导入模块: from keras.layers import merge [as 别名]

    # 或者: from keras.layers.merge import Add [as 别名]

    def conv_block(x0, scale):

    x = Conv2D(int(64*scale), (1, 1))(x0)

    x = InstanceNormalization()(x)

    x = LeakyReLU()(x)

    x = Conv2D(int(64*scale), (3, 3), padding='same')(x)

    x = InstanceNormalization()(x)

    x = LeakyReLU()(x)

    x = Conv2D(int(256*scale), (1, 1))(x)

    x = InstanceNormalization()(x)

    x1 = Conv2D(int(256*scale), (1, 1))(x0)

    x1 = InstanceNormalization()(x1)

    x = Add()([x, x1])

    x = LeakyReLU()(x)

    return x

    开发者ID:alecGraves,项目名称:cyclegan_keras,代码行数:20,

    示例4: residual_short

    ​点赞 5

    # 需要导入模块: from keras.layers import merge [as 别名]

    # 或者: from keras.layers.merge import Add [as 别名]

    def residual_short(prev_layer, level, pad=1, lvl=1, sub_lvl=1, modify_stride=False):

    prev_layer = Activation('relu')(prev_layer)

    block_1 = residual_conv(prev_layer, level,

    pad=pad, lvl=lvl, sub_lvl=sub_lvl,

    modify_stride=modify_stride)

    block_2 = short_convolution_branch(prev_layer, level,

    lvl=lvl, sub_lvl=sub_lvl,

    modify_stride=modify_stride)

    added = Add()([block_1, block_2])

    return added

    开发者ID:Vladkryvoruchko,项目名称:PSPNet-Keras-tensorflow,代码行数:13,

    示例5: residual_empty

    ​点赞 5

    # 需要导入模块: from keras.layers import merge [as 别名]

    # 或者: from keras.layers.merge import Add [as 别名]

    def residual_empty(prev_layer, level, pad=1, lvl=1, sub_lvl=1):

    prev_layer = Activation('relu')(prev_layer)

    block_1 = residual_conv(prev_layer, level, pad=pad,

    lvl=lvl, sub_lvl=sub_lvl)

    block_2 = empty_branch(prev_layer)

    added = Add()([block_1, block_2])

    return added

    开发者ID:Vladkryvoruchko,项目名称:PSPNet-Keras-tensorflow,代码行数:10,

    示例6: residual_short

    ​点赞 5

    # 需要导入模块: from keras.layers import merge [as 别名]

    # 或者: from keras.layers.merge import Add [as 别名]

    def residual_short(prev_layer, level, pad=1, lvl=1, sub_lvl=1,

    modify_stride=False):

    prev_layer = Activation('relu')(prev_layer)

    block_1 = residual_conv(prev_layer, level,

    pad=pad, lvl=lvl, sub_lvl=sub_lvl,

    modify_stride=modify_stride)

    block_2 = short_convolution_branch(prev_layer, level,

    lvl=lvl, sub_lvl=sub_lvl,

    modify_stride=modify_stride)

    added = Add()([block_1, block_2])

    return added

    开发者ID:divamgupta,项目名称:image-segmentation-keras,代码行数:14,

    示例7: _build_residual_block

    ​点赞 5

    # 需要导入模块: from keras.layers import merge [as 别名]

    # 或者: from keras.layers.merge import Add [as 别名]

    def _build_residual_block(self, x):

    mc = self.config.model

    in_x = x

    x = Conv2D(filters=mc.cnn_filter_num, kernel_size=mc.cnn_filter_size, padding="same",

    data_format="channels_first", kernel_regularizer=l2(mc.l2_reg))(x)

    x = BatchNormalization(axis=1)(x)

    x = Activation("relu")(x)

    x = Conv2D(filters=mc.cnn_filter_num, kernel_size=mc.cnn_filter_size, padding="same",

    data_format="channels_first", kernel_regularizer=l2(mc.l2_reg))(x)

    x = BatchNormalization(axis=1)(x)

    x = Add()([in_x, x])

    x = Activation("relu")(x)

    return x

    开发者ID:Zeta36,项目名称:connect4-alpha-zero,代码行数:15,

    示例8: add_images_with_tiled_vector_layer

    ​点赞 5

    # 需要导入模块: from keras.layers import merge [as 别名]

    # 或者: from keras.layers.merge import Add [as 别名]

    def add_images_with_tiled_vector_layer(images, vector, image_shape=None, vector_shape=None):

    """Tile a vector as if it were channels onto every pixel of an image.

    This version is designed to be used as layers within a Keras model.

    # Params

    images: a list of images to combine, must have equal dimensions

    vector: the 1D vector to tile onto every pixel

    image_shape: Tuple with 3 entries defining the shape (batch, height, width)

    images should be expected to have, do not specify the number

    of batches.

    vector_shape: Tuple with 3 entries defining the shape (batch, height, width)

    images should be expected to have, do not specify the number

    of batches.

    """

    with K.name_scope('add_images_with_tiled_vector_layer'):

    if not isinstance(images, list):

    images = [images]

    if vector_shape is None:

    # check if K.shape, K.int_shape, or vector.get_shape().as_list()[1:] is better

    # https://github.com/fchollet/keras/issues/5211

    vector_shape = K.int_shape(vector)[1:]

    if image_shape is None:

    # check if K.shape, K.int_shape, or image.get_shape().as_list()[1:] is better

    # https://github.com/fchollet/keras/issues/5211

    image_shape = K.int_shape(images[0])[1:]

    vector = Reshape([1, 1, vector_shape[-1]])(vector)

    tile_shape = (int(1), int(image_shape[0]), int(image_shape[1]), int(1))

    tiled_vector = Lambda(lambda x: K.tile(x, tile_shape))(vector)

    x = Add()([] + images + [tiled_vector])

    return x

    开发者ID:jhu-lcsr,项目名称:costar_plan,代码行数:33,

    示例9: TileOnto

    ​点赞 5

    # 需要导入模块: from keras.layers import merge [as 别名]

    # 或者: from keras.layers.merge import Add [as 别名]

    def TileOnto(x,z,zlen,xsize,add=False):

    z = Reshape([1,1,zlen])(z)

    tile_shape = (int(1), int(xsize[0]), int(xsize[1]), 1)

    z = Lambda(lambda x: K.tile(x, tile_shape))(z)

    if not add:

    x = Concatenate(axis=-1)([x,z])

    else:

    x = Add()([x,z])

    return x

    开发者ID:jhu-lcsr,项目名称:costar_plan,代码行数:11,

    示例10: DenseHelper

    ​点赞 5

    # 需要导入模块: from keras.layers import merge [as 别名]

    # 或者: from keras.layers.merge import Add [as 别名]

    def DenseHelper(x, dense_size, dropout_rate, repeat):

    '''

    Add a repeated number of dense layers of the same size.

    '''

    for i in range(repeat):

    if i < repeat - 1:

    dr = 0.

    else:

    dr = dropout_rate

    AddDense(x, dense_size, "relu", dr)

    return x

    开发者ID:jhu-lcsr,项目名称:costar_plan,代码行数:13,

    示例11: GetHuskyActorModel

    ​点赞 5

    # 需要导入模块: from keras.layers import merge [as 别名]

    # 或者: from keras.layers.merge import Add [as 别名]

    def GetHuskyActorModel(x, num_options, pose_size,

    dropout_rate=0.5, batchnorm=True):

    '''

    Make an "actor" network that takes in an encoded image and an "option"

    label and produces the next command to execute.

    '''

    xin = Input([int(d) for d in x.shape[1:]], name="actor_h_in")

    x0in = Input([int(d) for d in x.shape[1:]], name="actor_h0_in")

    pose_in = Input((pose_size,), name="actor_pose_in")

    option_in = Input((num_options,), name="actor_o_in")

    x = xin

    x0 = x0in

    dr, bn = dropout_rate, False

    use_lrelu = False

    x = Concatenate(axis=-1)([x, x0])

    x = AddConv2D(x, 32, [3,3], 1, dr, "same", lrelu=use_lrelu, bn=bn)

    # Add arm, gripper

    y = pose_in

    y = AddDense(y, 32, "relu", 0., output=True, constraint=3)

    x = TileOnto(x, y, 32, (8,8), add=False)

    x = AddConv2D(x, 64, [3,3], 1, dr, "valid", lrelu=use_lrelu, bn=bn)

    # Add arm, gripper

    y2 = AddDense(option_in, 64, "relu", 0., output=True, constraint=3)

    x = TileOnto(x, y2, 64, (6,6), add=False)

    x = AddConv2D(x, 128, [3,3], 1, dr, "valid", lrelu=use_lrelu, bn=bn)

    x = AddConv2D(x, 64, [3,3], 1, dr, "valid", lrelu=use_lrelu, bn=bn)

    x = Flatten()(x)

    x = AddDense(x, 512, "relu", dr, output=True, bn=bn)

    x = AddDense(x, 512, "relu", dr, output=True, bn=bn) # Same setup as the state decoders

    pose = AddDense(x, pose_size, "linear", 0., output=True)

    actor = Model([x0in, xin, option_in, pose_in], [pose], name="actor")

    return actor

    开发者ID:jhu-lcsr,项目名称:costar_plan,代码行数:41,

    示例12: GetHuskyPoseModel

    ​点赞 5

    # 需要导入模块: from keras.layers import merge [as 别名]

    # 或者: from keras.layers.merge import Add [as 别名]

    def GetHuskyPoseModel(x, num_options, pose_size,

    dropout_rate=0.5, batchnorm=True):

    '''

    Make an "actor" network that takes in an encoded image and an "option"

    label and produces the next command to execute.

    '''

    xin = Input([int(d) for d in x.shape[1:]], name="pose_h_in")

    x0in = Input([int(d) for d in x.shape[1:]], name="pose_h0_in")

    pose_in = Input((pose_size,), name="pose_pose_in")

    option_in = Input((num_options,), name="pose_o_in")

    x = xin

    x0 = x0in

    dr, bn = dropout_rate, False

    use_lrelu = False

    x = Concatenate(axis=-1)([x, x0])

    x = AddConv2D(x, 32, [3,3], 1, dr, "same", lrelu=use_lrelu, bn=bn)

    # Add arm, gripper

    y = pose_in

    y = AddDense(y, 32, "relu", 0., output=True, constraint=3)

    x = TileOnto(x, y, 32, (8,8), add=False)

    x = AddConv2D(x, 64, [3,3], 1, dr, "valid", lrelu=use_lrelu, bn=bn)

    # Add arm, gripper

    y2 = AddDense(option_in, 64, "relu", 0., output=True, constraint=3)

    x = TileOnto(x, y2, 64, (6,6), add=False)

    x = AddConv2D(x, 128, [3,3], 1, dr, "valid", lrelu=use_lrelu, bn=bn)

    x = AddConv2D(x, 64, [3,3], 1, dr, "valid", lrelu=use_lrelu, bn=bn)

    x = Flatten()(x)

    x = AddDense(x, 512, "relu", dr, output=True, bn=bn)

    x = AddDense(x, 512, "relu", dr, output=True, bn=bn) # Same setup as the state decoders

    pose = AddDense(x, pose_size, "linear", 0., output=True)

    pose = Model([x0in, xin, option_in, pose_in], [pose], name="pose")

    return pose

    开发者ID:jhu-lcsr,项目名称:costar_plan,代码行数:41,

    示例13: GetPoseModel

    ​点赞 5

    # 需要导入模块: from keras.layers import merge [as 别名]

    # 或者: from keras.layers.merge import Add [as 别名]

    def GetPoseModel(x, num_options, arm_size, gripper_size,

    dropout_rate=0.5, batchnorm=True):

    '''

    Make an "actor" network that takes in an encoded image and an "option"

    label and produces the next command to execute.

    '''

    img_shape = [int(d) for d in x.shape[1:]]

    img_in = Input(img_shape,name="policy_img_in")

    img0_in = Input(img_shape,name="policy_img0_in")

    arm = Input((arm_size,), name="ee_in")

    gripper = Input((gripper_size,), name="gripper_in")

    option_in = Input((48,), name="actor_o_in")

    ins = [img0_in, img_in, option_in, arm, gripper]

    x0, x = img0_in, img_in

    dr, bn = dropout_rate, False

    use_lrelu = False

    x = Concatenate(axis=-1)([x, x0])

    x = AddConv2D(x, 32, [3,3], 1, dr, "same", lrelu=use_lrelu, bn=bn)

    # Add arm, gripper

    y = Concatenate()([arm, gripper])

    y = AddDense(y, 32, "relu", 0., output=True, constraint=3)

    x = TileOnto(x, y, 32, (8,8), add=False)

    x = AddConv2D(x, 64, [3,3], 1, dr, "valid", lrelu=use_lrelu, bn=bn)

    # Add arm, gripper

    y2 = AddDense(option_in, 64, "relu", 0., output=True, constraint=3)

    x = TileOnto(x, y2, 64, (6,6), add=False)

    x = AddConv2D(x, 128, [3,3], 1, dr, "valid", lrelu=use_lrelu, bn=bn)

    x = AddConv2D(x, 64, [3,3], 1, dr, "valid", lrelu=use_lrelu, bn=bn)

    x = Flatten()(x)

    x = AddDense(x, 512, "relu", 0., output=True, bn=False)

    x = AddDense(x, 512, "relu", 0., output=True, bn=False) # Same setup as the state decoders

    arm = AddDense(x, arm_size, "linear", 0., output=True)

    gripper = AddDense(x, gripper_size, "sigmoid", 0., output=True)

    actor = Model(ins, [arm, gripper], name="pose")

    return actor

    开发者ID:jhu-lcsr,项目名称:costar_plan,代码行数:42,

    示例14: res_block

    ​点赞 5

    # 需要导入模块: from keras.layers import merge [as 别名]

    # 或者: from keras.layers.merge import Add [as 别名]

    def res_block(input_tensor, filters, kernel_size=(3, 3), strides=(1, 1), use_dropout=False):

    """实例化Keras Resnet块。

    Arguments:

    input_tensor {[type]} -- 输入张量

    filters {[type]} -- filters

    Keyword Arguments:

    kernel_size {tuple} -- [description] (default: {(3,3)})

    strides {tuple} -- [description] (default: {(1,1)})

    use_dropout {bool} -- [description] (default: {False})

    """

    x = ReflectionPadding2D((1, 1))(input_tensor)

    x = Conv2D(filters=filters, kernel_size=kernel_size, strides=strides)(x)

    x = BatchNormalization()(x)

    x = Activation("relu")(x)

    if use_dropout:

    x=Dropout(0.5)(x)

    x = ReflectionPadding2D((1, 1))(x)

    x = Conv2D(filters=filters, kernel_size=kernel_size, strides=strides)(x)

    x = BatchNormalization()(x)

    merged = Add()([input_tensor, x])

    return merged

    开发者ID:jarvisqi,项目名称:deep_learning,代码行数:28,

    示例15: resnet_block

    ​点赞 5

    # 需要导入模块: from keras.layers import merge [as 别名]

    # 或者: from keras.layers.merge import Add [as 别名]

    def resnet_block(n_filter, kernel_size=(3,3), pool=(1,1), n_conv_per_block=2,

    batch_norm=False, kernel_initializer='he_normal', activation='relu'):

    n_conv_per_block >= 2 or _raise(ValueError('required: n_conv_per_block >= 2'))

    len(pool) == len(kernel_size) or _raise(ValueError('kernel and pool sizes must match.'))

    n_dim = len(kernel_size)

    n_dim in (2,3) or _raise(ValueError('resnet_block only 2d or 3d.'))

    conv_layer = Conv2D if n_dim == 2 else Conv3D

    conv_kwargs = dict (

    padding = 'same',

    use_bias = not batch_norm,

    kernel_initializer = kernel_initializer,

    )

    channel_axis = -1 if backend_channels_last() else 1

    def f(inp):

    x = conv_layer(n_filter, kernel_size, strides=pool, **conv_kwargs)(inp)

    if batch_norm:

    x = BatchNormalization(axis=channel_axis)(x)

    x = Activation(activation)(x)

    for _ in range(n_conv_per_block-2):

    x = conv_layer(n_filter, kernel_size, **conv_kwargs)(x)

    if batch_norm:

    x = BatchNormalization(axis=channel_axis)(x)

    x = Activation(activation)(x)

    x = conv_layer(n_filter, kernel_size, **conv_kwargs)(x)

    if batch_norm:

    x = BatchNormalization(axis=channel_axis)(x)

    if any(p!=1 for p in pool) or n_filter != K.int_shape(inp)[-1]:

    inp = conv_layer(n_filter, (1,)*n_dim, strides=pool, **conv_kwargs)(inp)

    x = Add()([inp, x])

    x = Activation(activation)(x)

    return x

    return f

    开发者ID:CSBDeep,项目名称:CSBDeep,代码行数:42,

    示例16: identity_block

    ​点赞 5

    # 需要导入模块: from keras.layers import merge [as 别名]

    # 或者: from keras.layers.merge import Add [as 别名]

    def identity_block(x0, scale):

    x = Conv2D(int(64*scale), (1, 1))(x0)

    x = InstanceNormalization()(x)

    x = LeakyReLU()(x)

    x = Conv2D(int(64*scale), (3, 3), padding='same')(x)

    x = InstanceNormalization()(x)

    x = LeakyReLU()(x)

    x = Conv2D(int(256*scale), (1, 1))(x)

    x = InstanceNormalization()(x)

    x = Add()([x, x0])

    x = LeakyReLU()(x)

    return x

    开发者ID:alecGraves,项目名称:cyclegan_keras,代码行数:17,

    示例17: identity_block

    ​点赞 5

    # 需要导入模块: from keras.layers import merge [as 别名]

    # 或者: from keras.layers.merge import Add [as 别名]

    def identity_block(input_tensor, kernel_size, filters, stage, block,

    use_bias=True):

    """The identity_block is the block that has no conv layer at shortcut

    # Arguments

    input_tensor: input tensor

    kernel_size: default 3, the kernel size of middle conv layer at main path

    filters: list of integers, the nb_filters of 3 conv layer at main path

    stage: integer, current stage label, used for generating layer names

    block: 'a','b'..., current block label, used for generating layer names

    """

    nb_filter1, nb_filter2, nb_filter3 = filters

    conv_name_base = 'res' + str(stage) + block + '_branch'

    bn_name_base = 'bn' + str(stage) + block + '_branch'

    x = Conv2D(nb_filter1, (1, 1), name=conv_name_base + '2a',

    use_bias=use_bias)(input_tensor)

    x = BatchNorm(axis=3, name=bn_name_base + '2a')(x)

    x = Activation('relu')(x)

    x = Conv2D(nb_filter2, (kernel_size, kernel_size), padding='same',

    name=conv_name_base + '2b', use_bias=use_bias)(x)

    x = BatchNorm(axis=3, name=bn_name_base + '2b')(x)

    x = Activation('relu')(x)

    x = Conv2D(nb_filter3, (1, 1), name=conv_name_base + '2c',

    use_bias=use_bias)(x)

    x = BatchNorm(axis=3, name=bn_name_base + '2c')(x)

    x = Add()([x, input_tensor])

    x = Activation('relu', name='res' + str(stage) + block + '_out')(x)

    return x

    开发者ID:DeepinSC,项目名称:PyTorch-Luna16,代码行数:33,

    示例18: conv_block

    ​点赞 5

    # 需要导入模块: from keras.layers import merge [as 别名]

    # 或者: from keras.layers.merge import Add [as 别名]

    def conv_block(input_tensor, kernel_size, filters, stage, block,

    strides=(2, 2), use_bias=True):

    """conv_block is the block that has a conv layer at shortcut

    # Arguments

    input_tensor: input tensor

    kernel_size: defualt 3, the kernel size of middle conv layer at main path

    filters: list of integers, the nb_filters of 3 conv layer at main path

    stage: integer, current stage label, used for generating layer names

    block: 'a','b'..., current block label, used for generating layer names

    Note that from stage 3, the first conv layer at main path is with subsample=(2,2)

    And the shortcut should have subsample=(2,2) as well

    """

    nb_filter1, nb_filter2, nb_filter3 = filters

    conv_name_base = 'res' + str(stage) + block + '_branch'

    bn_name_base = 'bn' + str(stage) + block + '_branch'

    x = Conv2D(nb_filter1, (1, 1), strides=strides,

    name=conv_name_base + '2a', use_bias=use_bias)(input_tensor)

    x = BatchNorm(axis=3, name=bn_name_base + '2a')(x)

    x = Activation('relu')(x)

    x = Conv2D(nb_filter2, (kernel_size, kernel_size), padding='same',

    name=conv_name_base + '2b', use_bias=use_bias)(x)

    x = BatchNorm(axis=3, name=bn_name_base + '2b')(x)

    x = Activation('relu')(x)

    x = Conv2D(nb_filter3, (1, 1), name=conv_name_base +

    '2c', use_bias=use_bias)(x)

    x = BatchNorm(axis=3, name=bn_name_base + '2c')(x)

    shortcut = Conv2D(nb_filter3, (1, 1), strides=strides,

    name=conv_name_base + '1', use_bias=use_bias)(input_tensor)

    shortcut = BatchNorm(axis=3, name=bn_name_base + '1')(shortcut)

    x = Add()([x, shortcut])

    x = Activation('relu', name='res' + str(stage) + block + '_out')(x)

    return x

    开发者ID:DeepinSC,项目名称:PyTorch-Luna16,代码行数:39,

    示例19: compile

    ​点赞 5

    # 需要导入模块: from keras.layers import merge [as 别名]

    # 或者: from keras.layers.merge import Add [as 别名]

    def compile(self, learning_rate, momentum):

    """Gets the model ready for training. Adds losses, regularization, and

    metrics. Then calls the Keras compile() function.

    """

    # Optimizer object

    optimizer = keras.optimizers.SGD(lr=learning_rate, momentum=momentum,

    clipnorm=5.0)

    # Add Losses

    # First, clear previously set losses to avoid duplication

    self.keras_model._losses = []

    self.keras_model._per_input_losses = {}

    loss_names = ["rpn_class_loss", "rpn_bbox_loss",

    "mrcnn_class_loss", "mrcnn_bbox_loss", "mrcnn_mask_loss"]

    for name in loss_names:

    layer = self.keras_model.get_layer(name)

    if layer.output in self.keras_model.losses:

    continue

    self.keras_model.add_loss(

    tf.reduce_mean(layer.output, keep_dims=True))

    # Add L2 Regularization

    # Skip gamma and beta weights of batch normalization layers.

    reg_losses = [keras.regularizers.l2(self.config.WEIGHT_DECAY)(w) / tf.cast(tf.size(w), tf.float32)

    for w in self.keras_model.trainable_weights

    if 'gamma' not in w.name and 'beta' not in w.name]

    self.keras_model.add_loss(tf.add_n(reg_losses))

    # Compile

    self.keras_model.compile(optimizer=optimizer, loss=[

    None] * len(self.keras_model.outputs))

    # Add metrics for losses

    for name in loss_names:

    if name in self.keras_model.metrics_names:

    continue

    layer = self.keras_model.get_layer(name)

    self.keras_model.metrics_names.append(name)

    self.keras_model.metrics_tensors.append(tf.reduce_mean(

    layer.output, keep_dims=True))

    开发者ID:DeepinSC,项目名称:PyTorch-Luna16,代码行数:41,

    示例20: build

    ​点赞 5

    # 需要导入模块: from keras.layers import merge [as 别名]

    # 或者: from keras.layers.merge import Add [as 别名]

    def build(inp, encoder, nc, valid_shapes):

    side = conv_block_side(inp)

    x = Lambda(

    interp,

    arguments={'shape': valid_shapes[3]},

    name='sub24_sum_interp')(encoder)

    main = ConvBN(

    filters=128,

    kernel_size=3,

    dilation_rate=2,

    padding='same',

    name='conv_sub2')(x)

    x = Add(name='sub12_sum')([main, side])

    x = Activation('relu')(x)

    x = Lambda(

    interp,

    arguments={'shape': valid_shapes[2]},

    name='sub12_sum_interp')(x)

    x = Conv2D(

    filters=nc,

    kernel_size=1,

    name='conv6_cls')(x)

    out = Lambda(

    interp,

    arguments={'shape': valid_shapes[0]},

    name='conv6_interp')(x)

    return out

    开发者ID:PavlosMelissinos,项目名称:enet-keras,代码行数:36,

    示例21: identity_block

    ​点赞 5

    # 需要导入模块: from keras.layers import merge [as 别名]

    # 或者: from keras.layers.merge import Add [as 别名]

    def identity_block(kernel_size, filters, stage, block, weight_decay=0., batch_momentum=0.99):

    '''The identity_block is the block that has no conv layer at shortcut

    # Arguments

    kernel_size: defualt 3, the kernel size of middle conv layer at main path

    filters: list of integers, the nb_filters of 3 conv layer at main path

    stage: integer, current stage label, used for generating layer names

    block: 'a','b'..., current block label, used for generating layer names

    '''

    def f(input_tensor):

    nb_filter1, nb_filter2, nb_filter3 = filters

    if K.image_data_format() == 'channels_last':

    bn_axis = 3

    else:

    bn_axis = 1

    conv_name_base = 'res' + str(stage) + block + '_branch'

    bn_name_base = 'bn' + str(stage) + block + '_branch'

    x = Conv2D(nb_filter1, (1, 1), name=conv_name_base + '2a', kernel_regularizer=l2(weight_decay))(input_tensor)

    x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2a', momentum=batch_momentum)(x)

    x = Activation('relu')(x)

    x = Conv2D(nb_filter2, (kernel_size, kernel_size),

    padding='same', name=conv_name_base + '2b', kernel_regularizer=l2(weight_decay))(x)

    x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2b', momentum=batch_momentum)(x)

    x = Activation('relu')(x)

    x = Conv2D(nb_filter3, (1, 1), name=conv_name_base + '2c', kernel_regularizer=l2(weight_decay))(x)

    x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2c', momentum=batch_momentum)(x)

    x = Add()([x, input_tensor])

    x = Activation('relu')(x)

    return x

    return f

    开发者ID:aurora95,项目名称:Keras-FCN,代码行数:35,

    示例22: atrous_identity_block

    ​点赞 5

    # 需要导入模块: from keras.layers import merge [as 别名]

    # 或者: from keras.layers.merge import Add [as 别名]

    def atrous_identity_block(kernel_size, filters, stage, block, weight_decay=0., atrous_rate=(2, 2), batch_momentum=0.99):

    '''The identity_block is the block that has no conv layer at shortcut

    # Arguments

    kernel_size: defualt 3, the kernel size of middle conv layer at main path

    filters: list of integers, the nb_filters of 3 conv layer at main path

    stage: integer, current stage label, used for generating layer names

    block: 'a','b'..., current block label, used for generating layer names

    '''

    def f(input_tensor):

    nb_filter1, nb_filter2, nb_filter3 = filters

    if K.image_data_format() == 'channels_last':

    bn_axis = 3

    else:

    bn_axis = 1

    conv_name_base = 'res' + str(stage) + block + '_branch'

    bn_name_base = 'bn' + str(stage) + block + '_branch'

    x = Conv2D(nb_filter1, (1, 1), name=conv_name_base + '2a', kernel_regularizer=l2(weight_decay))(input_tensor)

    x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2a', momentum=batch_momentum)(x)

    x = Activation('relu')(x)

    x = Conv2D(nb_filter2, (kernel_size, kernel_size), dilation_rate=atrous_rate,

    padding='same', name=conv_name_base + '2b', kernel_regularizer=l2(weight_decay))(x)

    x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2b', momentum=batch_momentum)(x)

    x = Activation('relu')(x)

    x = Conv2D(nb_filter3, (1, 1), name=conv_name_base + '2c', kernel_regularizer=l2(weight_decay))(x)

    x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2c', momentum=batch_momentum)(x)

    x = Add()([x, input_tensor])

    x = Activation('relu')(x)

    return x

    return f

    开发者ID:aurora95,项目名称:Keras-FCN,代码行数:35,

    示例23: atrous_conv_block

    ​点赞 5

    # 需要导入模块: from keras.layers import merge [as 别名]

    # 或者: from keras.layers.merge import Add [as 别名]

    def atrous_conv_block(kernel_size, filters, stage, block, weight_decay=0., strides=(1, 1), atrous_rate=(2, 2), batch_momentum=0.99):

    '''conv_block is the block that has a conv layer at shortcut

    # Arguments

    kernel_size: defualt 3, the kernel size of middle conv layer at main path

    filters: list of integers, the nb_filters of 3 conv layer at main path

    stage: integer, current stage label, used for generating layer names

    block: 'a','b'..., current block label, used for generating layer names

    '''

    def f(input_tensor):

    nb_filter1, nb_filter2, nb_filter3 = filters

    if K.image_data_format() == 'channels_last':

    bn_axis = 3

    else:

    bn_axis = 1

    conv_name_base = 'res' + str(stage) + block + '_branch'

    bn_name_base = 'bn' + str(stage) + block + '_branch'

    x = Conv2D(nb_filter1, (1, 1), strides=strides,

    name=conv_name_base + '2a', kernel_regularizer=l2(weight_decay))(input_tensor)

    x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2a', momentum=batch_momentum)(x)

    x = Activation('relu')(x)

    x = Conv2D(nb_filter2, (kernel_size, kernel_size), padding='same', dilation_rate=atrous_rate,

    name=conv_name_base + '2b', kernel_regularizer=l2(weight_decay))(x)

    x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2b', momentum=batch_momentum)(x)

    x = Activation('relu')(x)

    x = Conv2D(nb_filter3, (1, 1), name=conv_name_base + '2c', kernel_regularizer=l2(weight_decay))(x)

    x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2c', momentum=batch_momentum)(x)

    shortcut = Conv2D(nb_filter3, (1, 1), strides=strides,

    name=conv_name_base + '1', kernel_regularizer=l2(weight_decay))(input_tensor)

    shortcut = BatchNormalization(axis=bn_axis, name=bn_name_base + '1', momentum=batch_momentum)(shortcut)

    x = Add()([x, shortcut])

    x = Activation('relu')(x)

    return x

    return f

    开发者ID:aurora95,项目名称:Keras-FCN,代码行数:40,

    示例24: GetJigsawsNextModel

    ​点赞 4

    # 需要导入模块: from keras.layers import merge [as 别名]

    # 或者: from keras.layers.merge import Add [as 别名]

    def GetJigsawsNextModel(x, num_options, dense_size, dropout_rate=0.5, batchnorm=True):

    '''

    Next actions

    '''

    xin = Input([int(d) for d in x.shape[1:]], name="Nx_prev_h_in")

    option_in = Input((1,), name="Nx_prev_o_in")

    x = xin

    x0 = x0in

    if len(x.shape) > 2:

    # Project

    x = AddConv2D(x, 32, [1,1], 1, dropout_rate, "same",

    bn=batchnorm,

    lrelu=True,

    name="Nx_project",

    constraint=None)

    x0 = AddConv2D(x0, 32, [1,1], 1, dropout_rate, "same",

    bn=batchnorm,

    lrelu=True,

    name="Nx_project0",

    constraint=None)

    x = Add()([x,x0])

    if num_options > 0:

    option_x = OneHot(num_options)(option_in)

    option_x = Flatten()(option_x)

    x = TileOnto(x, option_x, num_options, x.shape[1:3])

    # conv down

    x = AddConv2D(x, 64, [3,3], 1, dropout_rate, "valid",

    bn=batchnorm,

    lrelu=True,

    name="Nx_C64A",

    constraint=None)

    x = AddConv2D(x, 32, [3,3], 1, dropout_rate, "valid",

    bn=batchnorm,

    lrelu=True,

    name="Nx_C32A",

    constraint=None)

    # This is the hidden representation of the world, but it should be flat

    # for our classifier to work.

    x = Flatten()(x)

    # Next options

    x1 = AddDense(x, dense_size, "relu", dropout_rate, constraint=None,

    output=False,)

    x1 = AddDense(x1, dense_size, "relu", 0., constraint=None,

    output=False,)

    next_option_out = Dense(num_options,

    activation="sigmoid", name="lnext",)(x1)

    next_model = Model([x0in, xin, option_in], next_option_out, name="next")

    #next_model = Model([xin, option_in], next_option_out, name="next")

    return next_model

    开发者ID:jhu-lcsr,项目名称:costar_plan,代码行数:57,

    示例25: AddDense

    ​点赞 4

    # 需要导入模块: from keras.layers import merge [as 别名]

    # 或者: from keras.layers.merge import Add [as 别名]

    def AddDense(x, size, activation, dropout_rate, output=False, momentum=MOMENTUM,

    constraint=3,

    bn=True,

    kr=0.,

    ar=0.,

    perm_drop=False):

    '''

    Add a single dense block with batchnorm and activation.

    Parameters:

    -----------

    x: input tensor

    size: number of dense neurons

    activation: activation fn to use

    dropout_rate: dropout to use after activation

    Returns:

    --------

    x: output tensor

    '''

    if isinstance(kr, float) and kr > 0:

    kr = keras.regularizers.l2(kr)

    elif isinstance(kr, float):

    kr = None

    else:

    kr = kr

    if isinstance(ar, float) and ar > 0:

    ar = keras.regularizers.l1(ar)

    elif isinstance(ar, float):

    ar = None

    else:

    ar = ar

    if constraint is not None:

    x = Dense(size, kernel_constraint=maxnorm(constraint),

    kernel_regularizer=kr,

    activity_regularizer=ar,)(x)

    else:

    x = Dense(size,

    kernel_regularizer=kr,

    activity_regularizer=ar,)(x)

    if not output and bn:

    #x = BatchNormalization(momentum=momentum)(x)

    x = InstanceNormalization()(x)

    if activation == "lrelu":

    x = LeakyReLU(alpha=0.2)(x)

    else:

    x = Activation(activation)(x)

    if dropout_rate > 0:

    if perm_drop:

    x = PermanentDropout(dropout_rate)(x)

    else:

    x = Dropout(dropout_rate)(x)

    return x

    开发者ID:jhu-lcsr,项目名称:costar_plan,代码行数:60,

    示例26: TileArmAndGripper

    ​点赞 4

    # 需要导入模块: from keras.layers import merge [as 别名]

    # 或者: from keras.layers.merge import Add [as 别名]

    def TileArmAndGripper(x, arm_in, gripper_in, tile_width, tile_height,

    option=None, option_in=None,

    time_distributed=None, dim=64,

    concatenate=True):

    arm_size = int(arm_in.shape[-1])

    gripper_size = int(gripper_in.shape[-1])

    # handle error: options and grippers

    if option is None and option_in is not None \

    or option is not None and option_in is None:

    raise RuntimeError('must provide both #opts and input')

    # generate options and tile things together

    if option is None:

    robot = CombineArmAndGripper(arm_in, gripper_in, dim=dim)

    #reshape_size = arm_size+gripper_size

    reshape_size = dim

    else:

    robot = CombineArmAndGripperAndOption(arm_in,

    gripper_in,

    option_in,

    dim=dim)

    reshape_size = dim

    #reshape_size = arm_size+gripper_size+option

    # time distributed or not

    robot0 = robot

    if time_distributed is not None and time_distributed > 0:

    tile_shape = (1, 1, tile_width, tile_height, 1)

    robot = Reshape([time_distributed, 1, 1, reshape_size])(robot)

    else:

    tile_shape = (1, tile_width, tile_height, 1)

    robot = Reshape([1, 1, reshape_size])(robot)

    # finally perform the actual tiling

    robot = Lambda(lambda x: K.tile(x, tile_shape))(robot)

    if concatenate:

    x = Concatenate(axis=-1)([x,robot])

    else:

    x = Add()([x, robot])

    return x, robot0

    开发者ID:jhu-lcsr,项目名称:costar_plan,代码行数:44,

    示例27: GetActorModel

    ​点赞 4

    # 需要导入模块: from keras.layers import merge [as 别名]

    # 或者: from keras.layers.merge import Add [as 别名]

    def GetActorModel(x, num_options, arm_size, gripper_size,

    dropout_rate=0.5, batchnorm=True):

    '''

    Make an "actor" network that takes in an encoded image and an "option"

    label and produces the next command to execute.

    '''

    xin = Input([int(d) for d in x.shape[1:]], name="actor_h_in")

    x0in = Input([int(d) for d in x.shape[1:]], name="actor_h0_in")

    arm_in = Input((arm_size,), name="ee_in")

    gripper_in = Input((gripper_size,), name="gripper_in")

    option_in = Input((48,), name="actor_o_in")

    use_lrelu = False

    #dr, bn = dropout_rate, batchnorm

    x0, x = x0in, xin

    dr, bn = dropout_rate, False

    use_lrelu = False

    x = Concatenate(axis=-1)([x, x0])

    x = AddConv2D(x, 32, [3,3], 1, dr, "same", lrelu=use_lrelu, bn=bn)

    # Add arm, gripper

    y = Concatenate()([arm_in, gripper_in])

    y = AddDense(y, 32, "relu", 0., output=True, constraint=3)

    x = TileOnto(x, y, 32, (8,8), add=False)

    x = AddConv2D(x, 64, [3,3], 1, dr, "valid", lrelu=use_lrelu, bn=bn)

    # Add arm, gripper

    y2 = AddDense(option_in, 64, "relu", 0., output=True, constraint=3)

    x = TileOnto(x, y2, 64, (6,6), add=False)

    x = AddConv2D(x, 128, [3,3], 1, dr, "valid", lrelu=use_lrelu, bn=bn)

    x = AddConv2D(x, 64, [3,3], 1, dr, "valid", lrelu=use_lrelu, bn=bn)

    x = Flatten()(x)

    x = AddDense(x, 512, "relu", dr, output=True, bn=bn)

    x = AddDense(x, 512, "relu", dr, output=True, bn=bn) # Same setup as the state decoders

    arm = AddDense(x, arm_size, "linear", 0., output=True, bn=False)

    gripper = AddDense(x, gripper_size, "sigmoid", 0., output=True, bn=False)

    #value = Dense(1, activation="sigmoid", name="V",)(x1)

    actor = Model([x0in, xin, arm_in, gripper_in, option_in], [arm, gripper], name="actor")

    return actor

    开发者ID:jhu-lcsr,项目名称:costar_plan,代码行数:44,

    示例28: custom_unet

    ​点赞 4

    # 需要导入模块: from keras.layers import merge [as 别名]

    # 或者: from keras.layers.merge import Add [as 别名]

    def custom_unet(input_shape,

    last_activation,

    n_depth=2,

    n_filter_base=16,

    kernel_size=(3,3,3),

    n_conv_per_depth=2,

    activation="relu",

    batch_norm=False,

    dropout=0.0,

    pool_size=(2,2,2),

    n_channel_out=1,

    residual=False,

    prob_out=False,

    eps_scale=1e-3):

    """ TODO """

    if last_activation is None:

    raise ValueError("last activation has to be given (e.g. 'sigmoid', 'relu')!")

    all((s % 2 == 1 for s in kernel_size)) or _raise(ValueError('kernel size should be odd in all dimensions.'))

    channel_axis = -1 if backend_channels_last() else 1

    n_dim = len(kernel_size)

    conv = Conv2D if n_dim==2 else Conv3D

    input = Input(input_shape, name = "input")

    unet = unet_block(n_depth, n_filter_base, kernel_size,

    activation=activation, dropout=dropout, batch_norm=batch_norm,

    n_conv_per_depth=n_conv_per_depth, pool=pool_size)(input)

    final = conv(n_channel_out, (1,)*n_dim, activation='linear')(unet)

    if residual:

    if not (n_channel_out == input_shape[-1] if backend_channels_last() else n_channel_out == input_shape[0]):

    raise ValueError("number of input and output channels must be the same for a residual net.")

    final = Add()([final, input])

    final = Activation(activation=last_activation)(final)

    if prob_out:

    scale = conv(n_channel_out, (1,)*n_dim, activation='softplus')(unet)

    scale = Lambda(lambda x: x+np.float32(eps_scale))(scale)

    final = Concatenate(axis=channel_axis)([final,scale])

    return Model(inputs=input, outputs=final)

    开发者ID:CSBDeep,项目名称:CSBDeep,代码行数:46,

    注:本文中的keras.layers.merge.Add方法示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。

    展开全文
    weixin_39581972 2020-12-19 11:34:30
  • Merge函数的用法简单来说Merge函数相当于Excel中的vlookup函数。当我们对2个表进行数据合并的时候需要通过指定两个表中相同的列作为key,然后通过key匹配到其中要合并在一起的values值。然后对于merge函数在Pandas中...

    Merge函数的用法

    简单来说Merge函数相当于Excel中的vlookup函数。当我们对2个表进行数据合并的时候需要通过指定两个表中相同的列作为key,然后通过key匹配到其中要合并在一起的values值。

    然后对于merge函数在Pandas中分为1vs1, 多(m)vs1,以及多(m)vs多(m)这三种场景。但是平时用的最多的往往是多vs1的这种场景。也就是说2个表中其中一个表作为key的值会出现重复,而另外一个表作为key的值则是唯一。

    这种场景也很好理解。例如:我们在生产环境中对服务器进行管理,一台服务器上可能装了各种各样的软件。那么如果是Excel表格来管理的话一个软件就占用一行信息。而服务器名是相同的。所以一个相同的服务器名就会出现多个。

    这台服务器上安装了多少个软件,服务器名就会重复几次,也就是最终有几行。那么另外一个表要想读取这台服务器上安装的所有软件,那么服务器名就要作为key,各个软件的信息则是value值。最终被读取写入的那张表的key只能唯一。

    我们看下面这个案例,是真实多v1的案例。为了数据安全我只能把截图分享给大家,并把服务器名遮掩希望大家谅解。

    数据表1:作为查询的总表,其中服务器名这列就是B列中的信息会出现重复

    数据表2:下表为按照表1的key就是hostname来匹配,匹配到后按照表2的列名来读取信息写入到表2.这里同上因为服务器名敏感,所以也用马赛克挡住了,忘谅解。

    代码演示:

    1. 读取表1,表2中的内容,作为DataFrame赋值给变量

    #%%

    import pandas as pd

    #读取表1

    df01 = pd.read_excel("./datas/new_all_datas.xlsx",

    header=5)

    df01.head()

    #%%

    #读取表2

    df02 = pd.read_excel("./datas/new_software_InputSheet.xlsx")

    df02

    #%%

    2. 通过merge函数合并两个DataFrame。on代表指明拿什么作为key来进行匹配。how这里分为left,right,inner,outer等方式。这里left代表按照表1为主表进行合并。

    #%%

    #ホスト名作为key来匹配两个表,相当于vlookup函数

    #how=left代表以left左表为主,这里则代表表二为左表

    df03 = pd.merge(df02,df01,on="ホスト名",how="left")

    df03

    结果:合并结果如下。但是因为表1,表2中出现重复元素的列名,因此合并后Pandas会按照后缀,把相同列名按照_x,_y的方式生成多列。

    3. 去除没用的列并按照条件查询想要的数据. 下列需求是按照服务器名,找出对应的"ソフトウェア名"也就是software名为Trend Micro的软件以及"ソフトウェア?造元"software制造商为Symantec的所有行。

    #%%

    #因为表中有相同的列名因此自动后缀被加上了_y

    #下面代表筛选查询范围,以及指定查询值

    df03 = df03.loc[:,["ホスト名","行番号","ソフトウェア区分_y","ソフトウェア名_y","ソフトウェアバ?ジョン_y","ソフトウェア?造元_y"]]

    df04 = df03[(df03["ソフトウェア名_y"]=="Trend Micro") | (df03["ソフトウェア?造元_y"]=="Symantec")]

    #重新把列名设定换一下然后输出

    df04.columns = ["ホスト名","行番号","ソフトウェア区分","ソフトウェア名","ソフトウェアバ?ジョン","ソフトウェア?造元"]

    df04

    结果:

    4. 将数据导出到Excel文件

    #%%

    with pd.ExcelWriter("./datas/output_mergedatas.xlsx") as writer:

    df04.to_excel(writer,index=False)

    print("Done!!")

    结果:当然也可以直接导入到数据表2中去。我这里为了不破坏原表,因此作为新的Excel表导出了。

    大家在日常业务中,如果遇到类似场景可以尝试通过merge函数来合并您的数据。还可以结合loc切片以及写下来要发表的pivot,pivot_table透视表来更加丰富的对数据进行清洗。

    总体而言用惯了Pandas后会感觉相比Excel中的函数及宏。Pandas会更加的灵活也更加的强大。

    以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持得牛网。

    展开全文
    weixin_28694295 2020-12-23 13:07:22
  • /merage# ...merge(left, right, how=‘inner’, on=None, left_on=None, right_on=None, left_index=False, right_index=False, sort=True, suffixes=(’_x’, ‘_y’), copy=True, indicator=False) 作为
    1. /merage#
      pandas提供了一个类似于关系数据库的连接(join)操作的方法merage,可以根据一个或多个键将不同DataFrame中的行连接起来,语法如下:
      merge(left, right, how=‘inner’, on=None, left_on=None, right_on=None,
      left_index=False, right_index=False, sort=True,
      suffixes=(’_x’, ‘_y’), copy=True, indicator=False)
      作为一个功能完善、强大的语言,python的pandas库中的merge()支持各种内外连接。
      left与right:两个不同的DataFrame
      how:指的是合并(连接)的方式有inner(内连接),left(左外连接),right(右外连接),outer(全外连接);默认为inner
      on : 指的是用于连接的列索引名称。必须存在右右两个DataFrame对象中,如果没有指定且其他参数也未指定则以两个DataFrame的列名交集做为连接键
      left_on:左则DataFrame中用作连接键的列名;这个参数中左右列名不相同,但代表的含义相同时非常有用。
      right_on:右则DataFrame中用作 连接键的列名
      left_index:使用左则DataFrame中的行索引做为连接键
      right_index:使用右则DataFrame中的行索引做为连接键
      sort:默认为True,将合并的数据进行排序。在大多数情况下设置为False可以提高性能
      suffixes:字符串值组成的元组,用于指定当左右DataFrame存在相同列名时在列名后面附加的后缀名称,默认为(’_x’,’_y’)
      copy:默认为True,总是将数据复制到数据结构中;大多数情况下设置为False可以提高性能
      indicator:在 0.17.0中还增加了一个显示合并数据中来源情况;如只来自己于左边(left_only)、两者(both)
    #coding=utf-8
    import pandas as pd
    import numpy as np
    
    class PanMerge():
        def PanMer(self):
            data =  pd.DataFrame([{"id": 0, "name": 'lxh', "age": 20, "cp": 'lm'}, {"id": 1, "name": 'xiao', "age": 40, "cp": 'ly'},{"id": 2, "name": 'hua', "age": 4, "cp": 'yry'}, {"id": 3, "name": 'be', "age": 70, "cp": 'old'}])
            data1 = pd.DataFrame([{"id": 100, "name": 'lxh', 'cs': 10}, {"id": 101, "name": 'xiao', 'cs': 40},{"id": 102, "name": 'hua2', 'cs': 50}])
            data2 = pd.DataFrame([{"id": 0, "name": 'lxh', 'cs': 10}, {"id": 101, "name": 'xiao', 'cs': 40},{"id": 102, "name": 'hua2', 'cs': 50}])
            data3 = pd.DataFrame([{"mid": 0, "mname": 'lxh', 'cs': 10}, {"mid": 101, "mname": 'xiao', 'cs': 40},{"mid": 102, "mname": 'hua2', 'cs': 50}])
            # print(data)
            # print(data1)
            # print(data2)
            df1 = pd.merge(data,data1,on="name",how="left",suffixes=('_a','_b'))#相同的其他类名用_a和_b标注
            df2 = pd.merge(data, data2, on=("name", "id")) #多列名做为内链接的连接键
            df3 = pd.merge(data, data2)  #不指定on则以两个DataFrame的列名交集做为连接键
    
            # 使用右边的DataFrame的行索引做为连接键
            indexed_data1 = data1.set_index("name")##设置行索引名称
            # print(indexed_data1)
            df5 = pd. merge(data, indexed_data1, left_on='name', right_index=True) #"使用右边的DataFrame的行索引做为连接键\r\n"
            print(df5)
            print('左外连接\r\n',pd.merge(data,data1,on="name",how="left",suffixes=('_a','_b')))
            print('左外连接1\r\n',pd.merge(data1,data,on="name",how="left"))
            print ('右外连接\r\n',pd.merge(data,data1,on="name",how="right"))
    
            # 当左右两个DataFrame的列名不同,当又想做为连接键时可以使用left_on与right_on来指定连接键
            df6=pd.merge(data,data3,left_on=["name","id"],right_on=["mname","mid"])
            print(df6)
    

    join方法提供了一个简便的方法用于将两个DataFrame中的不同的列索引合并成为一个DataFrame。
    其中参数的意义与merge方法基本相同, 只是join方法默认为左外连接how = left。

     dj1=pd.DataFrame([{"id":0,"name":'lxh',"age":20,"cp":'lm'},{"id":1,"name":'xiao',"age":40,"cp":'ly'},{"id":2,"name":'hua',"age":4,"cp":'yry'},{"id":3,"name":'be',"age":70,"cp":'old'}],index=['a','b','c','d'])
        dj2=pd.DataFrame([{"sex":0},{"sex":1},{"sex":2}],index=['a','b','e'])
        print(dj1)
        print(dj2)
        df7= dj1.join(dj2)
        print(df7)
    
        print('使用右连接\r\n', dj1.join(dj2, how="right") ) # 这里出自动屏蔽了data1中没有index=c,d的那行数据;等价于data1.join(data)
        print('使用内连接\r\n', dj1.join(dj2, how='inner'))
        print('使用全外连接\r\n', dj1.join(dj2, how='outer'))
    

    还有一种连接方式:concat
    concat方法相当于数据库中的全连接(UNION ALL),可以指定按某个轴进行连接,也可以指定连接的方式join(outer,inner 只有这两种)。
    与数据库不同的是concat不会去重,要达到去重的效果可以使用drop_duplicates方法

         dc1 = pd.DataFrame({'city': ['Chicago', 'San Francisco', 'New York City'], 'rank': range(1, 4)})
            dc2 = pd.DataFrame({'city': ['Chicago', 'Boston', 'Los Angeles'], 'rank': [1, 4, 5]})
            print(dc1)
            print(dc2)
            # print('按轴进行内连接\r\n', pd.concat([dc1, dc2], join="inner", axis=0))
            dc3=pd.concat([dc1,dc2],join="inner", axis=0) #axis=1横向操作,axis=0纵向操作
            print(dc3)
            dc4=pd.concat([dc1,dc2],keys=['a','b']) #进行外连接并指定keys(行索引) 用a,b 进行标识
            print(dc4)
    
            dc5 = pd.concat([dc1,dc2],ignore_index=True).drop_duplicates() #完全一样时候,去重数据
            print(dc5)
    
    
    if __name__ == '__main__':
        PanMerge().PanMer()
    
    展开全文
    weixin_46576686 2021-06-16 17:11:29
  • weixin_29692653 2021-01-14 05:19:24
  • weixin_33736514 2020-12-23 13:07:24
  • marraybug 2021-06-16 19:06:18
  • weixin_39937412 2020-12-19 11:34:28
  • weixin_39557797 2020-12-22 12:04:09
  • weixin_31018573 2021-04-26 17:28:19
  • weixin_31209173 2021-02-03 09:29:38
  • weixin_39598796 2021-03-05 19:30:00
  • weixin_34862004 2021-04-26 17:29:13
  • weixin_42292348 2020-12-23 13:07:40
  • weixin_26765363 2021-02-09 04:14:19
  • weixin_29053577 2021-04-26 17:28:30
  • u011234030 2021-01-20 17:20:15
  • Isabella_leeo 2021-07-01 11:12:27
  • weixin_33364627 2021-01-14 13:49:36
  • weixin_39929566 2020-12-19 11:34:31
  • qq_44390640 2021-02-05 15:03:40
  • weixin_42395498 2021-02-09 05:11:21
  • yangl7 2021-03-30 16:48:51
  • weixin_36206388 2021-07-16 12:49:58
  • yyy199869 2021-12-09 18:51:59
  • sinat_32872729 2021-08-13 14:52:06
  • aaa123_456aaa 2021-11-12 23:47:22

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 82,223
精华内容 32,889
关键字:

pythonmerge

python 订阅