小编Gus*_*ust的帖子

如何在 conda 上安装 torchtext 0.4.0

torchtext 0.4.0 库存在(可以通过 pip 下载),但conda install torchtext=0.4.0不起作用。如何将 torchtext 下载到 anaconda 环境?

anaconda pytorch torchtext

7
推荐指数
2
解决办法
4220
查看次数

设置 LSTM 层的初始状态

我有以下代码:

units = 1024
lstm_layer = tf.keras.layers.LSTM(units)

dim = tf.zeros([64,1024])

output, hidden = lstm_layer(embedded_data, initial_state = dim)
Run Code Online (Sandbox Code Playgroud)

我收到以下错误消息:

ValueError: An `initial_state` was passed that is 
not compatible with `cell.state_size`.
 Received `state_spec`=
ListWrapper([InputSpec(shape=(64, 1024), ndim=2)]);
 however `cell.state_size` is [1024, 1024]
Run Code Online (Sandbox Code Playgroud)

当我使用 GRU 单元而不是 LSTM 单元执行此操作时,它工作正常。但是对于 LSTM 单元,此代码不起作用。我意识到 LSTM 需要两个参数,因此代码要求 [1024,1024] 的细胞状态,但我不知道如何设置初始状态。我试过

initial_state = [dim, dim] 
Run Code Online (Sandbox Code Playgroud)

这也不起作用,因为它给了我

ValueError: too many values to unpack (expected 2).
Run Code Online (Sandbox Code Playgroud)

从密集层引用了LSTM 初始状态,但似乎没有解决我的问题......

python keras tensorflow

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

未知的 CMake 命令“add_clang_executable”

我正在尝试构建一个 clang 工具,我正在尝试使用 CLion 对其进行调试。但是,我无法将其编译为独立的二进制文件。目前我在我的 CMakeLists.txt 中有这个:

add_clang_executable(clang_my_tool my_tool_util.h my_tool_util.cpp ClangMyTool.cpp)

target_link_libraries(clang_my_tool PRIVATE clangTooling)
Run Code Online (Sandbox Code Playgroud)

但是,它给了我错误消息:

add_clang_executable(clang_my_tool my_tool_util.h my_tool_util.cpp ClangMyTool.cpp)

target_link_libraries(clang_my_tool PRIVATE clangTooling)
Run Code Online (Sandbox Code Playgroud)

我知道我需要添加一个目录,但我不知道要添加哪个

我的llvm目录看起来像这样:

llvm
|-build
  |- ...
|
|-clang
  |-tools
    |-clang_my_tool
      |-ClangMyTool.cpp
      |-my_tool_util.h
      |-my_tool_util.c
      |-CMakeLists.txt
|- ... other directories...
Run Code Online (Sandbox Code Playgroud)

我要在 CMakeLists.txt 中添加什么?

c++ cmake llvm clang clion

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

短路列表,类型为 `(a -> Either ea) -> [a] -> Either e [a]` ... 单子操作?

考虑以下函数:

validateList :: (a -> Either e a) -> [a] -> Either e [a]
validateList validate []     = Right []
validateList validate (x:xs) =
    case validate x of
      Left err -> Left err
      Right y  -> case validateList validate xs of
                    Left err -> Left err
                    Right ys -> Right $ y:ys
Run Code Online (Sandbox Code Playgroud)

有没有办法以更简洁的方式写这个?也许使用>>=运算符?

需要考虑一下,因为这里实际上有两个 monad:[]Either,虽然List这里不充当 monad,但更多的是充当Traversable

monads haskell either

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

`(a -> m (Either eb)) -> Either ea -> m (Either eb)` 的一元函数?

有没有办法以更“单子”的方式编写这个函数,而不是诉诸于模式匹配Either

{-# LANGUAGE LambdaCase #-}

calculate :: (Monad m) => (a -> m (Either e b)) -> Either e a -> m (Either e b)
calculate f = \case
  Left err   -> return $ Left err
  Right vals -> f vals
Run Code Online (Sandbox Code Playgroud)

具体来说,对于我的用例来说,mIOf是一个函数,它接受输入并产生一些IO效果或失败的函数,而输入可能已经失败了。

也许使用ExceptT

monads haskell either

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

关联“数据项”的优雅方式

我试图将 of 的“数据项”ContentType与其内容相关联:

data ContentType = MyInt | MyBool deriving ( Show )

data Encoding'
  = EncodingInt  [Int]
  | EncodingBool [Bool]

chooseContentType :: IO ContentType
chooseContentType = undefined
Run Code Online (Sandbox Code Playgroud)

我如何制作这样的东西,但经过类型检查?

data Encoding a =
  Encoding { contentType :: ContentType
           , content :: [a]
           }
Run Code Online (Sandbox Code Playgroud)

haskell algebraic-data-types

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

如何在 Haskell 中使用来自不同类型 monad 的值

老实说,我觉得这必须有一个傻瓜的地方,但我找不到 甚至 搜索

假设我有以下代码来简单地从用户那里读取一个 double 并将其回显:

import qualified Control.Monad.Except as E
import Text.Read(readMaybe) 

data Error = ParseError String
             | Default String deriving (Show)

type ThrowsError = Either Error 

main = do
  putStrLn "Enter your number: "
  val <- getDouble
  print val

parseString :: String -> ThrowsError Double
parseString val = maybe (E.throwError $ ParseError val) return 
                        (readMaybe val :: Maybe Double)

getDouble :: ThrowsError Double
getDouble = getLine >>= parseString
Run Code Online (Sandbox Code Playgroud)

这在两个地方中断:

  1. main,putStrLn是类型IO …

monads haskell types

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

为什么C ++中的字符数组返回其项而不是其地址?

例如,

const char str[] ={'H','e','l','l','o', '\0'};
const int nums[] = {1,2,3,4,5};
cout << str << " " << nums;
Run Code Online (Sandbox Code Playgroud)

给出:

Hello 0x7ffff85c0cf5
Run Code Online (Sandbox Code Playgroud)

显然,如果我想同时获得两者的地址,我可以

Hello 0x7ffff85c0cf5
Run Code Online (Sandbox Code Playgroud)

但是我想知道为什么这个整数数组和一个字符数组(字符串文字)之间会有区别。这是仅保留给字符串的特殊情况吗?

c++ arrays string

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

对具有相同键的所有元组求和(实现 Haskell Data.Map.fromListWith)

我想对具有相同键的元组的值求和。

例如,

foo = [(True, 0), (True, 1), (False, 2), (True, -1), (False,4)] :: [(Bool, Int)]
putStrLn . show $ sumTuples foo
Run Code Online (Sandbox Code Playgroud)

应该显示 [(False,6),(True,0)].

当然,这很容易做到Data.Map.fromListWith

foo = [(True, 0), (True, 1), (False, 2), (True, -1), (False,4)] :: [(Bool, Int)]
putStrLn . show $ sumTuples foo
Run Code Online (Sandbox Code Playgroud)

但我想在没有帮助的情况下实现它Data.Map

使用过滤器,我可以这样解决:

sumTuples :: (Ord a, Num b) => [(a, b)] -> [(a, b)]
sumTuples = Map.toList . Map.fromListWith (+)
Run Code Online (Sandbox Code Playgroud)

或带有折叠:

sumByFilter :: [(Bool, Int)] -> [(Bool, Int)]
sumByFilter xs …
Run Code Online (Sandbox Code Playgroud)

dictionary haskell tuples

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