我正在用Python写一个神经网络,按照这里的例子.考虑到神经网络经过一万次训练后无法产生正确的值(在误差范围内),似乎反向传播算法不起作用.具体来说,我正在训练它来计算以下示例中的正弦函数:
import numpy as np
class Neuralnet:
def __init__(self, neurons):
self.weights = []
self.inputs = []
self.outputs = []
self.errors = []
self.rate = .1
for layer in range(len(neurons)):
self.inputs.append(np.empty(neurons[layer]))
self.outputs.append(np.empty(neurons[layer]))
self.errors.append(np.empty(neurons[layer]))
for layer in range(len(neurons)-1):
self.weights.append(
np.random.normal(
scale=1/np.sqrt(neurons[layer]),
size=[neurons[layer], neurons[layer + 1]]
)
)
def feedforward(self, inputs):
self.inputs[0] = inputs
for layer in range(len(self.weights)):
self.outputs[layer] = np.tanh(self.inputs[layer])
self.inputs[layer + 1] = np.dot(self.weights[layer].T, self.outputs[layer])
self.outputs[-1] = np.tanh(self.inputs[-1])
def backpropagate(self, targets):
gradient = 1 - self.outputs[-1] * self.outputs[-1]
self.errors[-1] …Run Code Online (Sandbox Code Playgroud) python numpy machine-learning backpropagation neural-network
考虑以下函数,它返回一组元素的所有唯一排列:
def get_permutations(elements):
if len(elements) == 0:
yield ()
else:
unique_elements = set(elements)
for first_element in unique_elements:
remaining_elements = list(elements)
remaining_elements.remove(first_element)
for subpermutation in get_permutations(tuple(remaining_elements)):
yield (first_element,) + subpermutation
for permutation in get_permutations((1, 1, 2)):
print(permutation)
Run Code Online (Sandbox Code Playgroud)
这打印
(1, 1, 2)
(1, 2, 1)
(2, 1, 1)
Run Code Online (Sandbox Code Playgroud)
正如所料.但是,当我添加lru_cache装饰器时,它会记住该函数:
import functools
@functools.lru_cache(maxsize=None)
def get_permutations(elements):
if len(elements) == 0:
yield ()
else:
unique_elements = set(elements)
for first_element in unique_elements:
remaining_elements = list(elements)
remaining_elements.remove(first_element)
for subpermutation in get_permutations(tuple(remaining_elements)):
yield (first_element,) + subpermutation …Run Code Online (Sandbox Code Playgroud) 我试图使用重载来使可变参数函数的返回类型以某种方式依赖于其参数的类型。具体来说,当且仅当其任何参数的类型为 X 时,我希望返回类型为X。
考虑以下最小示例:
from typing import overload
class Safe:
pass
class Dangerous:
pass
@overload
def combine(*args: Safe) -> Safe: ...
@overload
def combine(*args: Safe | Dangerous) -> Safe | Dangerous: ...
def combine(*args: Safe | Dangerous) -> Safe | Dangerous:
if all(isinstance(arg, Safe) for arg in args):
return Safe()
else:
return Dangerous()
reveal_type(combine())
reveal_type(combine(Safe()))
reveal_type(combine(Dangerous()))
reveal_type(combine(Safe(), Safe()))
reveal_type(combine(Safe(), Dangerous()))
Run Code Online (Sandbox Code Playgroud)
这输出
example.py:21: note: Revealed type is "example.Safe"
example.py:22: note: Revealed type is "example.Safe"
example.py:23: note: Revealed …Run Code Online (Sandbox Code Playgroud) 我正在实现一个解释器,允许用户定义任意组合器并将它们应用于任意术语.例如,用户可以通过输入以下组合子定义来定义对的Church编码:
pair a b c ? c a b
true a b ? a
first a ? a true
Run Code Online (Sandbox Code Playgroud)
然后用户可以输入first (pair a b),根据先前定义的规则逐步减少:
first (pair a b)
? pair a b true
? true a b
? a
Run Code Online (Sandbox Code Playgroud)
S x y z ? x z (y z)
K x y ? x
I x ? x
Run Code Online (Sandbox Code Playgroud)
身份组合子也可以在头两个组合子条款所限定I ? S S K K或I ? S K …
binary-tree interpreter functional-programming combinators combinatory-logic
通过代码分析,我发现Math.sqrt函数特别是在程序中每个时间步运行的大型双嵌套循环中的主要瓶颈。有什么办法可以改善其性能?我应该内联某种迭代计算还是基于查找表的计算?
任何帮助将不胜感激!
我不能用平方计算代替它,因为它不是比较。
编辑:代码的相关部分大致如下
var width = 2000;
var height = 2000;
function update() {
for (var j = 0; j < height; ++j) {
for (var i = 0; i < width; ++i) {
array[i][j] = Math.sqrt(/* some expression involving i and j */);
}
}
}
var fps = 60;
setInterval(update, 1000 / fps);
Run Code Online (Sandbox Code Playgroud) 我试图用C++中的SK组合子演算来实现K组合子.在K组合子是一个高阶函数,基本上需要一些值x,并返回一些东西,又需要一个值y,并返回x从它.换一种说法,
K(x)(y) == x
Run Code Online (Sandbox Code Playgroud)
或者一步一步:
intermediate = K(x)
intermediate(y) == x
Run Code Online (Sandbox Code Playgroud)
K(x)作为一种独立于自身的事物的能力y是必不可少的.此外,没有必要指定y何时简单地创建K(x)而不调用它y.y可以K(x)(y)在代码中的某处评估一次指定的类型.
我试图修复我写的试图实现K组合的代码:
#include <iostream>
template<class A>
template<class B>
auto K = [](A x) {
return [=](B y) {
return x;
};
};
int main()
{
std::cout << "Hello world!\n";
auto Kx = K<int>(3);
auto Kxy = Kx<float>(4.5);
std::cout << Kxy << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
它输出 …
c++ templates metaprogramming functor higher-order-functions
如何提高以下循环检测代码的性能
from matplotlib.pyplot import imshow, scatter, show
import cv2
image = cv2.imread('points.png', 0)
_, image = cv2.threshold(image, 254, 255, cv2.THRESH_BINARY)
image = cv2.Canny(image, 1, 1)
imshow(image, cmap='gray')
circles = cv2.HoughCircles(image, cv2.HOUGH_GRADIENT, 2, 32)
x = circles[0, :, 0]
y = circles[0, :, 1]
scatter(x, y)
show()
Run Code Online (Sandbox Code Playgroud)
使用以下源图像:
我已经尝试调整HoughCircles函数的参数但是它们会导致过多的误报或过多的误报.特别是,我遇到了在两个blob之间的间隙中检测到虚假圆圈的问题:
python ×4
binary-tree ×1
c++ ×1
caching ×1
combinators ×1
functools ×1
functor ×1
geometry ×1
interpreter ×1
javascript ×1
mypy ×1
numeric ×1
numpy ×1
opencv ×1
optimization ×1
overloading ×1
profiling ×1
python-3.x ×1
templates ×1
type-hinting ×1