我正在尝试在 O(N log K) 时间内解决Top K 常用词 Leetcode 问题,但得到了不希望的结果。我的 Python3 代码和控制台输出如下:
from collections import Counter
import heapq
class Solution:
def topKFrequent(self, words: List[str], k: int) -> List[str]:
counts = Counter(words)
print('Word counts:', counts)
result = []
for word in counts:
print('Word being added:', word)
if len(result) < k:
heapq.heappush(result, (-counts[word], word))
print(result)
else:
heapq.heappushpop(result, (-counts[word], word))
result = [r[1] for r in result]
return result
----------- Console output -----------
Word counts: Counter({'the': 3, 'is': 3, 'sunny': 2, 'day': …Run Code Online (Sandbox Code Playgroud) 该HuggingFace BERT TensorFlow实施使我们能够进在预先计算的地方嵌入查找是原产于BERT的嵌入。这是使用模型call方法的可选参数inputs_embeds(代替input_ids)完成的。为了测试这一点,我想确保如果我确实输入了 BERT 的嵌入查找,我会得到与输入input_ids它们自己相同的结果。
BERT 嵌入查找的结果可以通过将 BERT 配置参数设置output_hidden_states为True并从该call方法的最后一个输出中提取第一个张量来获得。(其余 12 个输出对应于 12 个隐藏层中的每一个。)
因此,我编写了以下代码来测试我的假设:
import tensorflow as tf
from transformers import BertConfig, BertTokenizer, TFBertModel
bert_tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
input_ids = tf.constant(bert_tokenizer.encode("Hello, my dog is cute", add_special_tokens=True))[None, :]
attention_mask = tf.stack([tf.ones(shape=(len(sent),)) for sent in input_ids])
token_type_ids = tf.stack([tf.ones(shape=(len(sent),)) for sent in input_ids])
config = BertConfig.from_pretrained('bert-base-uncased', output_hidden_states=True)
bert_model = TFBertModel.from_pretrained('bert-base-uncased', config=config)
result = bert_model(inputs={'input_ids': input_ids, …Run Code Online (Sandbox Code Playgroud) python nlp tensorflow bert-language-model huggingface-transformers
在 SO 上至少还有两个这样的问题,但没有一个得到回答。
我有以下形式的数据集:
<TensorSliceDataset shapes: ((512,), (512,), (512,), ()), types: (tf.int32, tf.int32, tf.int32, tf.int32)>
Run Code Online (Sandbox Code Playgroud)
和另一种形式:
<BatchDataset shapes: ((None, 512), (None, 512), (None, 512), (None,)), types: (tf.int32, tf.int32, tf.int32, tf.int32)>
Run Code Online (Sandbox Code Playgroud)
我看了又看,但找不到将这些数据集保存到以后可以加载的文件的代码。我得到的最接近的是TensorFlow 文档中的这个页面,它建议使用序列化张量tf.io.serialize_tensor,然后使用tf.data.experimental.TFRecordWriter.
但是,当我使用代码尝试此操作时:
dataset.map(tf.io.serialize_tensor)
writer = tf.data.experimental.TFRecordWriter('mydata.tfrecord')
writer.write(dataset)
Run Code Online (Sandbox Code Playgroud)
我在第一行收到错误:
类型错误:serialize_tensor() 需要 1 到 2 个位置参数,但给出了 4 个
我如何修改上述(或做其他事情)以实现我的目标?
我想知道是否有办法为Mac OS 10.5编写最小化和恢复窗口的代码.它会是什么语言?有人可以给我一个例子,或者直接告诉我Apple应该关注的开发者网站上的文档吗?
谢谢!
有没有办法在MATLAB中对以下循环进行矢量化?
for j = 1:length(cursor_bin)
cursor_bin(j) = mean(cursor(bin == j));
end
Run Code Online (Sandbox Code Playgroud)
cursor_bin,cursor和bin所有载体.
在通过 BERT 传递我的代币之前,我想对它们的嵌入(嵌入查找层的结果)执行一些处理。HuggingFace BERT TensorFlow 实现允许我们使用以下方式访问嵌入查找的输出:
import tensorflow as tf
from transformers import BertConfig, BertTokenizer, TFBertModel
bert_tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
input_ids = tf.constant(bert_tokenizer.encode("Hello, my dog is cute", add_special_tokens=True))[None, :]
attention_mask = tf.stack([tf.ones(shape=(len(sent),)) for sent in input_ids])
token_type_ids = tf.stack([tf.ones(shape=(len(sent),)) for sent in input_ids])
config = BertConfig.from_pretrained('bert-base-uncased', output_hidden_states=True)
bert_model = TFBertModel.from_pretrained('bert-base-uncased', config=config)
result = bert_model(inputs={'input_ids': input_ids,
'attention_mask': attention_mask,
'token_type_ids': token_type_ids})
inputs_embeds = result[-1][0] # output of embedding lookup
Run Code Online (Sandbox Code Playgroud)
随后,可以inputs_embeds使用以下方法处理并将其作为输入发送到同一模型:
inputs_embeds = process(inputs_embeds) # some processing on inputs_embeds done here …Run Code Online (Sandbox Code Playgroud) python nlp tensorflow bert-language-model huggingface-transformers
我正在使用XCode版本3.1.2,正在使用Leopard上的iOS 2.2.1模拟器开发iPhone.我的项目中有一个名为"img.jpg"的图像文件,我决定换一个不同的文件.将新文件添加到XCode Resources文件夹后,我删除了第一个文件,并将新文件重命名为与前一个文件相同的名称"img.jpg".但是,当我运行程序时,模拟器会加载旧图像而不是新图像,即使旧图像已从磁盘中删除(而不仅仅是参考).我尝试将文件的名称更改为"img2.jpg",它的工作原理应该是 - 加载新图像,但我想保留名称"img.jpg".我使用Spotlight搜索"img.jpg"以查看是否存在XCode正在使用的另一个副本,但它只返回了我的新图像文件.我尝试从模拟器卸载应用程序并再次运行应用程序,但这也无法解决问题.
我必须做些什么才能让XCode认识到我想要使用新的图像文件而不是旧的图像文件?
谢谢你的帮助!!
我正在使用 svmtrain 来区分几对数据。尽管 svmtrain 在一种情况下按预期工作(输出一个分类器对象,其准确度为 svmclassify 验证的约 70%),但所有其他情况似乎都失败了。我的特征向量是 134 维,我为每个类使用了 300 到 800 个数据点。(每个类不一定具有相同数量的数据点)。我已经尝试使用该方法为 svmtrain 使用默认内核
SVM = svmtrain(double(train{k}), group_train{k},'showplot',true);
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我收到错误:
无法解决优化问题:超过最大迭代次数;增加 options.MaxIter。要以当前解为起点继续求解问题,请在调用 quadprog 之前设置 x0 = x。
我还尝试使用以下调用扩展迭代次数并指定内核:
options = optimset('maxiter',1000,'largescale','on');
SVM = svmtrain(double(train{k}),group_train{k},'Kernel_Function','mlp','Method','QP',...
'quadprog_opts',options);
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我收到错误:
无法解决优化问题:退出:解无界且无穷大;约束不够严格。
在确实有效的情况下,我有来自第一类的 338 个数据点和来自第二类的 476 个数据点。例如,在三个不起作用的情况下,我在第二个类中有 828、573 和 333 个数据点,而第一个类保持不变,有 338 个数据点。两种方法调用似乎都不起作用。
请你帮助我好吗?我一直试图解决这个问题一个星期,但没有运气。我在具有 1 GHz 处理器和 2 GB RAM 的虚拟机 Windows XP 上使用 MATLAB 7.9.0 R2009B。
非常感谢!-维维克
我想知道为什么对于此处( train_en.txt) 和此处( )给出的文件,使用 bash 进行的简单行计数给出的行数与使用 python(版本 3.6)计算的行数不同train_de.txt。在 bash 中,我使用以下命令:
wc -l train_en.txt
wc -l train_de.txt
Run Code Online (Sandbox Code Playgroud)
输出分别为 4520620 和 4520620。
在 python 中,我使用以下命令:
print(sum(1 for line in open('train_en.txt')))
print(sum(1 for line in open('train_de.txt')))
Run Code Online (Sandbox Code Playgroud)
输出分别为 4521327 和 4521186。
当我使用 python 命令时
len(open('train_en.txt').read().splitlines())
len(open('train_de.txt').read().splitlines())
Run Code Online (Sandbox Code Playgroud)
我分别得到 4521334 和 4521186(其train_en.txt结果与之前的 python 命令的结果不匹配)。
作为参考,这些是通过连接WMT '14 英语到德语翻译任务中的Common Crawl、Europarl和News Commentary数据集(按顺序)生成的并行文本语料库,并且应具有相同的行数。