小编Tob*_*ann的帖子

两个列表的值的组合

我正在寻找一种惯用的方法来将n维向量(作为列表给出)与偏移列表相结合,该列表应该应用于每个维度.即:鉴于我有以下值和偏移量:

v = [5, 6]
o = [-1, 2, 3]
Run Code Online (Sandbox Code Playgroud)

我想获得以下列表:

n = [[4, 5], [7, 5], [8, 5], [4, 8], [7, 8], [8, 8], [4, 9], [7, 9], [8, 9]]
Run Code Online (Sandbox Code Playgroud)

起源于:

n = [[5-1, 6-1], [5+2, 6-1], [5+3, 6-1], [5-1, 6+2], [5+2, 6+2], [5+3, 6+2], [5-1, 6+3], [5+2, 6+3], [5+3, 6+3]]
Run Code Online (Sandbox Code Playgroud)

性能在这里不是问题,结果列表的顺序也无关紧要.关于如何在没有丑陋的嵌套for循环的情况下生成这个的任何建议?我想itertools提供了解决方案的工具,但我还没弄明白.

python combinations idiomatic

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

将函数列表一个接一个地应用于值

是否有内置函数将函数列表一个接一个地应用于值?我目前正在使用它,但它对我来说似乎很不优雅.

-- applyFunctions [(*2), (+3), (*4)] 1 == ((1 * 2) + 3 ) * 4
applyFunctions :: [(a -> a)] -> a -> a
applyFunctions [] x = x
applyFunctions [f] x = f x
applyFunctions (f:fs) x = applyFunctions fs (f x)
Run Code Online (Sandbox Code Playgroud)

haskell

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

如何重用不同类型的IO提升功能?

以下代码

import Control.Applicative
import Control.Arrow
import Data.List.Split

main :: IO ()
main = do
    ints <- getNumberLine
    integers <- getNumberLine
    print $ foo ints integers

getNumberLine = readDataLine <$> getLine

readDataLine :: Read a => String -> [a]
readDataLine = splitOn " " >>> map read

foo :: [Int] -> [Integer] -> String
foo _ _ = "hello"
Run Code Online (Sandbox Code Playgroud)

给出此错误消息:

GetNumberLine.hs:9:22:
    Couldn't match type `Int' with `Integer'
    Expected type: [Integer]
      Actual type: [Int]
    In the second argument of `foo', namely `integers' …
Run Code Online (Sandbox Code Playgroud)

haskell type-inference

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

函数模板中返回类型的模板参数推导

给出以下最小例子:

#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <list>
#include <set>
#include <vector>

template<typename ContainerOut, typename X, typename Y, typename ContainerIn>
ContainerOut Map( const ContainerIn& xs, const std::function<Y( const X& )>& f )
{
    ContainerOut ys;
    std::transform( begin( xs ), end( xs ), std::inserter( ys, end( ys ) ), f );
    return ys;
}

struct Foo {
    Foo( int val ) : val_( val ) {}
    int val_;
};

std::set<int> FooValsToIntSet( const std::list<Foo>& foos )
{
    //Map<std::set<int>, Foo, …
Run Code Online (Sandbox Code Playgroud)

c++ c++11

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

理解Keras模型体系结构(嵌套模型的节点索引)

此脚本使用小型嵌套模型定义虚拟模型

from keras.layers import Input, Dense
from keras.models import Model
import keras

input_inner = Input(shape=(4,), name='input_inner')
output_inner = Dense(3, name='inner_dense')(input_inner)
inner_model = Model(inputs=input_inner, outputs=output_inner)

input = Input(shape=(5,), name='input')
x = Dense(4, name='dense_1')(input)
x = inner_model(x)
x = Dense(2, name='dense_2')(x)

output = keras.layers.concatenate([x, x], name='concat_1')
model = Model(inputs=input, outputs=output)

print(model.summary())
Run Code Online (Sandbox Code Playgroud)

产生以下输出

Layer (type)                     Output Shape          Param #     Connected to                     
====================================================================================================
input (InputLayer)               (None, 5)             0                                            
____________________________________________________________________________________________________
dense_1 (Dense)                  (None, 4)             24          input[0][0]                      
____________________________________________________________________________________________________
model_1 (Model)                  (None, 3)             15          dense_1[0][0]                    
____________________________________________________________________________________________________
dense_2 …
Run Code Online (Sandbox Code Playgroud)

deep-learning keras

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