class RBM():
def __init__(self, nv, nh):
self.W = torch.randn(nh, nv)
self.a = torch.randn(1, nh)
self.b = torch.randn(1, nv)
def sample_h(self, x):
wx = torch.mm(x, self.W.t())
activation = wx + self.a.expand_as(wx)
p_h_given_v = torch.sigmoid(activation)
return p_h_given_v, torch.bernoulli(p_h_given_v)
def sample_v(self, y):
wy = torch.mm(y, self.W)
activation = wy + self.b.expand_as(wy)
p_v_given_h = torch.sigmoid(activation)
return p_v_given_h, torch.bernoulli(p_v_given_h)
def train(self, v0, vk, ph0, phk):
self.W += torch.mm(v0.t(), ph0) - torch.mm(vk.t(), phk)
self.b += torch.sum((v0 - vk), 0)
self.a += torch.sum((ph0 - phk), 0)
Run Code Online (Sandbox Code Playgroud)
错误: …
python machine-learning python-3.x unsupervised-learning deep-learning
我有以下示例:
def func1():return 1
def func2():return 2
def func3():return 3
fl = [func1,func2,func3]
Run Code Online (Sandbox Code Playgroud)
我想按一定顺序调用函数; 例如
要么
怎么会发生这种情况?
def example():
var1 = {'a':1,'b':2}
var2 = {'c':3,'d':4}
return var1,var2
[v1, v2] = example()
Run Code Online (Sandbox Code Playgroud)
我想将var1分配给v1,将var2分配给v2.是否可以将tuple var1,var2解压缩到列表v1,v2中.到目前为止,我还没有找到任何人将多个返回值解压缩到列表中
我有一个包含英文字母,印地语字母,希腊符号和数字的列表。我要删除除印地语以外的所有字母。Unicode中的北印度语字母范围是u'0900'-u'097F'。有关印地语字母的详细信息,请访问http://jrgraphix.net/r/Unicode/0900-097F。
输入:
l=['?','1?','==?','@','??','abc123','?','?','abc??']
for i in l:
print i
Run Code Online (Sandbox Code Playgroud)
所需输出:
?
?
?
??
??
Run Code Online (Sandbox Code Playgroud) 我有一个元组生成器,我需要删除包含相同元素的元组。我需要此输出进行迭代。
Input = ((1, 1), (1, 2), (1, 3), (3, 1), (3, 2), (3, 3))
Output= ((1, 1), (1, 2), (1, 3))
Run Code Online (Sandbox Code Playgroud)
输出顺序无关紧要。
我已经检查了这个问题,但是它与列表有关:删除嵌套列表Python中具有相同元素的重复元组
我使用生成器来获得最快的结果,因为数据非常大。
我用这个库openpyxl.
并将数据读取为:
for row in sheet.rows:
print row[0].value
Run Code Online (Sandbox Code Playgroud)
而不是细胞的价值我得到公式:=D42-D42*0.1.
我有下面的类和其中定义的功能。
class utils:
def pass_hash(unhashed):
hashed = hashlib.sha256(unhashed)
hashed = hashed.hexdigest()
return hashed
Run Code Online (Sandbox Code Playgroud)
当我打电话
print(utils.pass_hash('abc'.encode()))
Run Code Online (Sandbox Code Playgroud)
它工作正常,但如果我打电话
obj = utils()
print(obj.pass_hash('abc'.encode()))
Run Code Online (Sandbox Code Playgroud)
它给出以下错误:
print(obj.pass_hash('abc'.encode()))
TypeError: pass_hash() takes 1 positional argument but 2 were given
Run Code Online (Sandbox Code Playgroud)
而如果我在函数中传递自变量,则这种行为会逆转,即它可以很好地与对象配合使用,但是在访问时像utils.pass_hash()一样会给出错误。
有人可以解释一下这种行为吗?
为什么在交互式控制台打印输出中运行此代码?
>>> def a():
... return 1
...
>>> for i in range(3):
... a()
...
1
1
1
>>>
Run Code Online (Sandbox Code Playgroud)
我希望没有输出.这种行为记录在哪里?
尝试运行几天前正在运行的代码时出现奇怪的错误。添加了根本不使用str()并得到的小功能
UnboundLocalError:赋值前引用了局部变量“str”
其结果。引用的部分根本没有被触及。
这是那个片段。(如果需要更多我的代码,我将编辑我的帖子):
def get_level_stats(number,version,prev_uniques):
starts = events.find({"eventName":"Level " + str(number),"gameVersion":{"$in": version }, "status": "start"})
fails = events.find({"eventName":"Level " + str(number),"gameVersion":{"$in": version }, "status": "failed"})
finishes = events.find({"eventName":"Level " + str(number),"gameVersion":{"$in":version }, "status": "finish"})
players_dict = {}
for result in starts:
try:
players_dict[result['uid']]
players_dict[result['uid']]["starts"] = players_dict[result['uid']]["starts"] +1
players_dict[result['uid']]["last_day"] = result['updated_at']
except:
players_dict[result['uid']] = {}
players_dict[result['uid']]["starts"] = 1
players_dict[result['uid']]["finish"] = 0
players_dict[result['uid']]["booster"] = 0
players_dict[result['uid']]["extra"] = 0
players_dict[result['uid']]["last_day"] = result['updated_at']
for result in finishes:
try:
players_dict[result['uid']]
players_dict[result['uid']]["finish"] = …Run Code Online (Sandbox Code Playgroud) 我多次尝试将 CSV 文件导入 python 2.7.15,但失败了。请建议如何在 Python 中导入 CSV。

谢谢
我是 Python 新手,有以下问题
>>> choice = [1,0,1,1,]
>>> choice = [1,0,1,1]
>>> print(choice)
[1, 0, 1, 1]
>>> print(choice[2])
1
Run Code Online (Sandbox Code Playgroud)
为什么它打印 1 而不是 0?
python ×11
python-3.x ×4
python-2.7 ×3
csv ×1
function ×1
generator ×1
hindi ×1
openpyxl ×1
python-2.x ×1
python-3.5 ×1
tuples ×1
unicode ×1