我想根据存储在另一个列表中的可变迭代次数和作为整数存储的恒定跳过次数来迭代给定列表。
假设我有 3 件事 -
l - 我需要迭代(或过滤)的列表w - 一个列表,告诉我休息前要迭代多少项k - 一个整数,告诉我在每组迭代之间跳过多少个元素。换个说法,w 告诉要进行多少次迭代,在每组迭代之后,k 告诉要跳过多少个元素。
所以,如果 w = [4,3,1] 并且 k = 2。然后在给定的列表(长度为 14)上,我想迭代前 4 个元素,然后跳过 2,然后是接下来的 3 个元素,然后跳过 2,然后下一个元素,然后跳过 2。
另一个例子,
#Lets say this is my original list
l = [6,2,2,5,2,5,1,7,9,4]
w = [2,2,1,1]
k = 1
Run Code Online (Sandbox Code Playgroud)
基于 w 和 k,我想迭代为 -
6 -> Keep # w says keep 2 elements
2 -> Keep
2 -> Skip # k says skip 1
5 -> Keep # w …Run Code Online (Sandbox Code Playgroud) 这是使用 EMLo 获取嵌入的代码。
import tensorflow_hub as hub
import tensorflow as tf
elmo = hub.Module("https://tfhub.dev/google/elmo/2")
x = ["Roasted ants are a popular snack in Columbia"]
embeddings = elmo(x, signature="default", as_dict=True)["elmo"] # To Extract ELMo features
embeddings.shape
Run Code Online (Sandbox Code Playgroud)
我收到这种类型的错误,type error : pruned(text): expected argument #0(zero-based) to be a Tensor; got list (['Roasted ants are a popular snack in Columbia'])。
pd.read_clipboard()当在 SO 上发布的数据在列之间有空格时,我很乐意使用( '\s\s+')
然而,如何将下表直接复制到 pandas 数据框呢?
| 记录ID | 共享于 (UNIX 时间戳) |
股份类型 | 分享给用户 |
|---|---|---|---|
| 1 | 1611872850 | 共享 | 用户A |
| 2 | 1611872851 | 共享 | 用户B |
| 3 | 1611872852 | 共享 | 用户B |
| 1 | 1611872853 | 共享已删除 | 用户A |
我尝试找到一些可以帮助我解决这个问题的东西,而我最接近复制此数据框的是使用以下内容,其中有很多不必要的列和不必要的空间,我稍后必须使用它们来df.col.str.strip()清理。
#Clicking edit on the question, and copying the underlying markdown
pd.read_clipboard('|')
Run Code Online (Sandbox Code Playgroud)
Unnamed: 0 Record ID Shared On<br/>(UNIX timestamp) \
0 NaN ------------ ------------
1 NaN 1 1611872850
2 NaN 2 1611872851
3 NaN 3 1611872852
4 NaN 1 1611872853
Share type Share To User Unnamed: 5 …Run Code Online (Sandbox Code Playgroud) 我正在尝试实现一个 CNN-LSTM,对代表帕金森病/健康控制者语音的梅尔谱图图像进行分类。我正在尝试使用 LSTM 模型实现一个预先存在的模型 (DenseNet-169),但是我遇到了以下错误:ValueError: Input 0 of layer zero_padding2d is incompatible with the layer: expected ndim=4, found ndim=3. Full shape received: [None, 216, 1].任何人都可以告诉我哪里出了问题吗?
import librosa
import os
import glob
import IPython.display as ipd
from pathlib import Path
import timeit
import time, sys
%matplotlib inline
import matplotlib.pyplot as plt
import librosa.display
import pandas as pd
from sklearn import datasets, linear_model
from sklearn.model_selection import train_test_split
from matplotlib import pyplot as plt
import numpy as np
import cv2
import …Run Code Online (Sandbox Code Playgroud) 我正在尝试为我的分层数据实现 Poincar\xc3\xa9 嵌入,如 Facebook 的一篇论文(链接)中所讨论的。您可以在此处找到 Poincar\xc3\xa9 嵌入的更易于理解的解释。
\n根据这篇论文,我在这里和这里找到了 Tensorflow 的一些实现,以及Tensorflow Addons 中的tfa.layers.PoincareNormalize。后者甚至有上面提到的论文的链接,这让我相信这对我来说可能是一个很好的起点。然而,到目前为止,我没有成功实现 tfa.layers.PoincareNormalize,除了我链接的 API 页面上的一些通用信息之外,也找不到任何文档。
\n有谁知道应该如何实现该层以提供论文中讨论的双曲空间中的嵌入?我的出发点是使用标准嵌入层的实现,如下所示(它实际上是分类变量的实体嵌入)?
\ninput = Input(shape=(1, ))\nmodel = Embedding(input_dim=my_input_dim, \n output_dim=embed_dim, name="my_feature")(input)\nmodel = Reshape(target_shape=(embed_dim, ))(model)\nmodel = Dense(1)(model)\nmodel = Activation(\'sigmoid\')(model)\nRun Code Online (Sandbox Code Playgroud)\n由于输入不同,仅用 tfa.layers.PoincareNormalize 替换 Embedding 层是行不通的。我假设它可以放置在嵌入层之后的某个位置,以便对于反向传播步骤,“值”在每次迭代时都投影到双曲空间中,但到目前为止也没有运气。
\n我正在尝试在anaconda py3.6上安装Fancyimpute,赢得10,64位.得到以下错误.
Collecting fancyimpute
Requirement already satisfied: downhill in c:\anaconda3\lib\site-packages (from fancyimpute)
Requirement already satisfied: numpy>=1.10 in c:\anaconda3\lib\site-packages (from fancyimpute)
Requirement already satisfied: scikit-learn>=0.17.1 in c:\anaconda3\lib\site-packages (from fancyimpute)
Requirement already satisfied: theano in c:\anaconda3\lib\site-packages (from fancyimpute)
Requirement already satisfied: scipy in c:\anaconda3\lib\site-packages (from fancyimpute)
Requirement already satisfied: climate in c:\anaconda3\lib\site-packages (from fancyimpute)
Requirement already satisfied: knnimpute in c:\anaconda3\lib\site-packages (from fancyimpute)
Requirement already satisfied: six in c:\anaconda3\lib\site-packages (from fancyimpute)
Collecting cvxpy (from fancyimpute)
Using cached cvxpy-0.4.10-py3-none-any.whl
Requirement already satisfied: click in …Run Code Online (Sandbox Code Playgroud) 我想获得字符串的动名词形式。我还没有找到调用库来获取动名词的直接方法。
我应用了以“ing”结尾的单词的规则,但是因为异常导致我收到了一些错误。然后,我检查 cmu 单词以确保生成的动名词单词正确。代码如下:
import cmudict
import re
ing= 'ing'
vowels = "aeiou"
consonants = "bcdfghjklmnpqrstvwxyz"
words=['lead','take','hit','begin','stop','refer','visit']
cmu_words= cmudict.words()
g_w = []
for word in words:
if word[-1] == 'e':
if word[:-1] + ing in cmu_words:
g_w.append(word[:-1] + ing)
elif count_syllables(word) == 1 and word[-2] in vowels and word[-1] in consonants:
if word.__len__()>2 and word[-3] in vowels:
if word + ing in cmu_words:
g_w.append(word + ing)
else:
if word + word[-1] + ing in cmu_words:
g_w.append(word + word[-1] + ing) …Run Code Online (Sandbox Code Playgroud) 我第一次构建用于图像分类的 CNN 模型,我对每种类型(1D CNN、2D CNN、3D CNN)的输入形状以及如何固定滤波器中的滤波器数量感到有点困惑。卷积层。我的数据是 100x100x30,其中 30 是特征。这是我使用函数式 API Keras 编写的 1D CNN 文章:
def create_CNN1D_model(pool_type='max',conv_activation='relu'):
input_layer = (30,1)
conv_layer1 = Conv1D(filters=16, kernel_size=3, activation=conv_activation)(input_layer)
max_pooling_layer1 = MaxPooling1D(pool_size=2)(conv_layer1)
conv_layer2 = Conv1D(filters=32, kernel_size=3, activation=conv_activation)(max_pooling_layer1)
max_pooling_layer2 = MaxPooling1D(pool_size=2)(conv_layer2)
flatten_layer = Flatten()(max_pooling_layer2)
dense_layer = Dense(units=64, activation='relu')(flatten_layer)
output_layer = Dense(units=10, activation='softmax')(dense_layer)
CNN_model = Model(inputs=input_layer, outputs=output_layer)
return CNN_model
CNN1D = create_CNN1D_model()
CNN1D.compile(loss = 'categorical_crossentropy', optimizer = "adam",metrics = ['accuracy'])
Trace = CNN1D.fit(X, y, epochs=50, batch_size=100)
Run Code Online (Sandbox Code Playgroud)
然而,在尝试通过将 Conv1D、Maxpooling1D 更改为 Conv2D 和 Maxpooling2D 来尝试 2D CNN 模型时,我收到以下错误: …
我正在恢复这个 github问题,因为我相信它是有效的并且需要解释。tf.keras 有一个掩码层,其文档如下:
对于输入张量中的每个时间步长(张量中的维度#1),如果该时间步长的输入张量中的所有值都等于 mask_value,则该时间步长将在所有下游层中被屏蔽(跳过)(只要它们支持掩蔽)。
如果任何下游层不支持掩码但收到这样的输入掩码,则会引发异常。
# create padded zeros and change two valid entries.
inputs = np.zeros([1,5])
inputs[0,1] = 0.5
inputs[0,2] = 0.1
inputs = tf.Variable(inputs)
masked_inputs = tf.keras.layers.Masking(mask_value=0.0)(inputs)
with_masking = tf.keras.layers.Softmax()(masked_inputs)
without_masking = tf.keras.layers.Softmax()(inputs)
Run Code Online (Sandbox Code Playgroud)
两个结果几乎相同
with_masking
<tf.Tensor: shape=(1, 5), dtype=float32, numpy=
array([[0.1737954 , 0.28654018, 0.19207363, 0.1737954 , 0.1737954 ]],
dtype=float32)>
without_masking
<tf.Tensor: shape=(1, 5), dtype=float64, numpy=array([[0.1737954 , 0.28654017, 0.19207362, 0.1737954 , 0.1737954 ]])>
Run Code Online (Sandbox Code Playgroud)
我希望只对有效条目进行 softmax 处理,类似于
#Assign one large value
inputs = np.zeros([1,2])
inputs[0,0] = …Run Code Online (Sandbox Code Playgroud) 我有一个列表 X,其中包含列表列表的列表...例如
X = [ [[], [], [], []], [[], [] ,[], []] ]
Run Code Online (Sandbox Code Playgroud)
当我尝试使用 将此列表(X)转换为 numpy 数组时
np.array(),我得到以下结果:
array([list([[],[],[],[]]),
list([list([[],[],[],[]]),....)
Run Code Online (Sandbox Code Playgroud)
这个列表是双关语*。
我在这里做错了什么?我只需要它以正常的数组形式,这样我就可以以列表不允许的方式索引它(即array[:,1])
我遇到了一个用于torch.einsum计算张量乘法的代码。我能够理解低阶张量的工作原理,但是不能理解 4D 张量的工作原理,如下所示:
import torch
a = torch.rand((3, 5, 2, 10))
b = torch.rand((3, 4, 2, 10))
c = torch.einsum('nxhd,nyhd->nhxy', [a,b])
print(c.size())
# output: torch.Size([3, 2, 5, 4])
Run Code Online (Sandbox Code Playgroud)
我需要以下方面的帮助:
torch.einsum在这种情况下实际上有好处吗?multiline = "Life is too short\nYou need python."
multiline #1
#'Life is too short\nYou need python.'
print(multiline) #2
#Life is too short
#You need python.
Run Code Online (Sandbox Code Playgroud)
我不知道为什么#1 的结果不能显示#2 的相同结果。
python ×11
tensorflow ×4
keras ×3
python-3.x ×2
anaconda ×1
embedding ×1
fancyimpute ×1
lstm ×1
max-pooling ×1
nlp ×1
nltk ×1
numpy ×1
pandas ×1
pip ×1
pytorch ×1
tensor ×1