小编Ara*_*Fey的帖子

在对Boltzmann机器执行对比发散时,出现以下错误。我正在使用最新版本的pytorch

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

-1
推荐指数
1
解决办法
764
查看次数

Python以特定顺序从列表中调用函数

我有以下示例:

def func1():return 1
def func2():return 2
def func3():return 3

fl = [func1,func2,func3]
Run Code Online (Sandbox Code Playgroud)

我想按一定顺序调用函数; 例如

  • func1(),然后是func2(),最后是func3()

要么

  • func2(),然后是func1(),最后是func3()

怎么会发生这种情况?

python function

-1
推荐指数
1
解决办法
48
查看次数

python中的函数多次返回

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中.到目前为止,我还没有找到任何人将多个返回值解压缩到列表中

python python-2.7

-1
推荐指数
1
解决办法
63
查看次数

如何在Python中使用其unicode值打印字符?

我有一个包含英文字母,印地语字母,希腊符号和数字的列表。我要删除除印地语以外的所有字母。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)

python unicode python-2.x hindi

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

在生成器Python 3.5中删除与相同元素无关的重复元组

我有一个元组生成器,我需要删除包含相同元素的元组。我需要此输出进行迭代。

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中具有相同元素的重复元组

我使用生成器来获得最快的结果,因为数据非常大。

python tuples generator python-3.5

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

如何获得细胞的价值而不是公式值?

我用这个库openpyxl.

并将数据读取为:

for row in sheet.rows:
    print row[0].value
Run Code Online (Sandbox Code Playgroud)

而不是细胞的价值我得到公式:=D42-D42*0.1.

python python-2.7 openpyxl

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

如果要直接使用self而不是class对象,python函数是否需要self

我有下面的类和其中定义的功能。

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()一样会给出错误。

有人可以解释一下这种行为吗?

python python-3.x

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

Python循环输出到控制台

为什么在交互式控制台打印输出中运行此代码?

>>> def a():
...   return 1
...
>>> for i in range(3):
...   a()
...
1
1
1
>>>
Run Code Online (Sandbox Code Playgroud)

我希望没有输出.这种行为记录在哪里?

python

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

使用 str() 时出错

尝试运行几天前正在运行的代码时出现奇怪的错误。添加了根本不使用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)

python python-3.x

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

将 CSV 文件导入 Python

我多次尝试将 CSV 文件导入 python 2.7.15,但失败了。请建议如何在 Python 中导入 CSV。

将 csv 转换为 python

谢谢

python csv python-2.7

-4
推荐指数
1
解决办法
8906
查看次数

为什么 python 返回第 n+1 个列表元素而不是第 n 个?

我是 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 python-3.x

-6
推荐指数
1
解决办法
2万
查看次数