我试图调和我对LSTM的理解,并在克里斯托弗·奥拉在克拉拉斯实施的这篇文章中指出.我正在关注Jason Brownlee为Keras教程撰写的博客.我主要困惑的是,
[samples, time steps, features]和,让我们参考下面粘贴的代码集中讨论上述两个问题:
# reshape into X=t and Y=t+1
look_back = 3
trainX, trainY = create_dataset(train, look_back)
testX, testY = create_dataset(test, look_back)
# reshape input to be [samples, time steps, features]
trainX = numpy.reshape(trainX, (trainX.shape[0], look_back, 1))
testX = numpy.reshape(testX, (testX.shape[0], look_back, 1))
########################
# The IMPORTANT BIT
##########################
# create and fit the LSTM network
batch_size = 1
model = Sequential()
model.add(LSTM(4, …Run Code Online (Sandbox Code Playgroud) 假设我有一个数据框,其国家/地区如下:
cc | temp
US | 37.0
CA | 12.0
US | 35.0
AU | 20.0
Run Code Online (Sandbox Code Playgroud)
我知道有一个pd.get_dummies函数可以将这些国家/地区转换为"单热编码".但是,我希望将它们转换为索引,以便我得到它cc_index = [1,2,1,3].
我假设有一种比使用get_dummies和numpy where子句更快的方法,如下所示:
[np.where(x) for x in df.cc.get_dummies().values]
使用'因子'在R中这样做有点容易,所以我希望大熊猫有类似的东西.
我正在尝试比较我的Doc2Vec(通过tf)和gensims实现的实现.从视觉上看,gensim的表现更好.
我运行以下代码来训练gensim模型和下面的那个用于tensorflow模型.我的问题如下:
window=5gensim中的参数是否意味着我在两边使用两个词来预测中间的一个?或者两边都是5.事情是有相当多的文件小于长度10.model = Doc2Vec(dm=1, dm_concat=1, size=100, window=5, negative=10, hs=0, min_count=2, workers=cores)
model.build_vocab(corpus)
epochs = 100
for i in range(epochs):
model.train(corpus)
Run Code Online (Sandbox Code Playgroud)
batch_size = 512
embedding_size = 100 # Dimension of the embedding vector.
num_sampled = 10 # Number of negative examples to sample.
graph = tf.Graph()
with graph.as_default(), tf.device('/cpu:0'):
# Input data.
train_word_dataset = tf.placeholder(tf.int32, shape=[batch_size])
train_doc_dataset = tf.placeholder(tf.int32, shape=[batch_size/context_window])
train_labels = tf.placeholder(tf.int32, shape=[batch_size/context_window, 1])
# The variables
word_embeddings = tf.Variable(tf.random_uniform([vocabulary_size,embedding_size],-1.0,1.0)) …Run Code Online (Sandbox Code Playgroud) 假设我有一个结构化数据帧,如下所示:
df = pd.DataFrame({"A":['a','a','a','b','b'],"B":[1]*5})
Run Code Online (Sandbox Code Playgroud)
该A列先前已经排序.我希望找到第一行索引的位置df[df.A!='a'].最终目标是使用此索引将数据框拆分为基于的组A.
现在我意识到有一个groupby功能.但是,数据帧非常大,这是一个简化的玩具示例.既然A已经排序了,如果我能找到第一个索引的位置会更快df.A!='a'.因此,重要的是,一旦找到第一个元素,您使用扫描的任何方法都会停止.
我正在尝试按照tensorflow上的udacity教程,我遇到了以下两行用于单词嵌入模型:
# Look up embeddings for inputs.
embed = tf.nn.embedding_lookup(embeddings, train_dataset)
# Compute the softmax loss, using a sample of the negative labels each time.
loss = tf.reduce_mean(tf.nn.sampled_softmax_loss(softmax_weights, softmax_biases,
embed, train_labels, num_sampled, vocabulary_size))
Run Code Online (Sandbox Code Playgroud)
现在我明白第二个陈述是对负标签进行采样.但问题是它如何知道负面标签是什么?我提供的第二个功能是当前输入及其相应的标签以及我想要(负面)采样的标签数量.是否存在从输入集中采样的风险?
这是完整的示例:https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/udacity/5_word2vec.ipynb
我正在尝试将标记生成器保存在 Huggingface 中,以便以后可以从不需要访问互联网的容器中加载它。
BASE_MODEL = "distilbert-base-multilingual-cased"
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
tokenizer.save_vocabulary("./models/tokenizer/")
tokenizer2 = AutoTokenizer.from_pretrained("./models/tokenizer/")
Run Code Online (Sandbox Code Playgroud)
但是,最后一行给出了错误:
OSError: Can't load config for './models/tokenizer3/'. Make sure that:
- './models/tokenizer3/' is a correct model identifier listed on 'https://huggingface.co/models'
- or './models/tokenizer3/' is the correct path to a directory containing a config.json file
Run Code Online (Sandbox Code Playgroud)
变压器版本:3.1.0
不幸的是,如何从 Pytorch 中的预训练模型加载保存的标记器并没有帮助。
感谢下面@ashwin 的回答,我save_pretrained改为尝试,但出现以下错误:
OSError: Can't load config for './models/tokenizer/'. Make sure that:
- './models/tokenizer/' is a correct model identifier listed on 'https://huggingface.co/models'
- or …Run Code Online (Sandbox Code Playgroud) 我希望使用LDA将每个文档分配给一个主题.现在我意识到你得到的是LDA主题的分布.但是,正如您从下面的最后一行所看到的,我将其分配给最可能的主题.
我的问题是这个.lda[corpus]为了得到这些话题,我必须第二次跑步.是否有一些其他内置gensim函数将直接给我这个主题赋值向量?特别是因为LDA算法已通过文档,它可能已经保存了这些主题分配?
# Get the Dictionary and BoW of the corpus after some stemming/ cleansing
texts = [[stem(word) for word in document.split() if word not in STOPWORDS] for document in cleanDF.text.values]
dictionary = corpora.Dictionary(texts)
dictionary.filter_extremes(no_below=5, no_above=0.9)
corpus = [dictionary.doc2bow(text) for text in texts]
# The actual LDA component
lda = models.LdaMulticore(corpus=corpus, id2word=dictionary, num_topics=30, chunksize=10000, passes=10,workers=4)
# Assign each document to most prevalent topic
lda_topic_assignment = [max(p,key=lambda item: item[1]) for p in lda[corpus]]
Run Code Online (Sandbox Code Playgroud) 以下是我对LSTM序列的基本序列的理解.假设我们正在处理问答环境.
你有两套LSTM(下面是绿色和蓝色).每组分别共享权重(即4个绿色单元中的每一个具有相同的权重并且与蓝色单元类似).第一个是多对一LSTM,它总结了最后一个隐藏层/单元内存的问题.
第二组(蓝色)是多对多LSTM,其与第一组LSTM具有不同的权重.输入只是答案句子,而输出是相同的句子移一.
问题有两个:1.我们是否将最后一个隐藏状态仅作为初始隐藏状态传递给蓝色LSTM.或者它是最后隐藏的状态和单元格记忆.2.有没有办法在Keras或Tensorflow中设置初始隐藏状态和单元格内存?如果是这样参考?
这是Subsetting Dask DataFrames的后续问题.我希望在将数据批量发送到ML算法之前对来自dask数据帧的数据进行混洗.
该问题的答案是做以下事情:
for part in df.repartition(npartitions=100).to_delayed():
batch = part.compute()
Run Code Online (Sandbox Code Playgroud)
然而,即使我要改变批次的内容,我也有点担心它可能不太理想.数据是一个时间序列集,因此数据点在每个分区内高度相关.
理想情况下我喜欢的是:
rand_idx = np.random.choice(len(df), batch_size, replace=False)
batch = df.iloc[rand_idx, :]
Run Code Online (Sandbox Code Playgroud)
哪个适用于熊猫,但不适用于dask.有什么想法吗?
我试过了
train_len = int(len_df*0.8)
idx = np.random.permutation(len_df)
train_idx = idx[:train_len]
test_idx = idx[train_len:]
train_df = df.loc[train_idx]
test_df = df.loc[test_idx]
Run Code Online (Sandbox Code Playgroud)
但是,如果我尝试这样做,则train_df.loc[:5,:].compute()返回一个124451行数据帧.所以显然使用dask错了.
假设我有以下张量:y = torch.randint(0, 3, (10,))。你会如何计算其中的 0、1 和 2?
我能想到的唯一方法是使用,collections.Counter(y)但想知道是否有更“pytorch”的方法来做到这一点。例如,一个用例是构建用于预测的混淆矩阵。