这是一个简单的代码工作实现,我在Python的scikit-learn中使用高斯过程回归(GPR),使用二维输入(即网格结束x1
和x2
)和1维输出(y
).
import numpy as np
from matplotlib import pyplot as plt
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import RBF, ConstantKernel as C
from mpl_toolkits.mplot3d import Axes3D
# Example independent variable (observations)
X = np.array([[0.,0.], [1.,0.], [2.,0.], [3.,0.], [4.,0.],
[5.,0.], [6.,0.], [7.,0.], [8.,0.], [9.,0.], [10.,0.],
[11.,0.], [12.,0.], [13.,0.], [14.,0.],
[0.,1.], [1.,1.], [2.,1.], [3.,1.], [4.,1.],
[5.,1.], [6.,1.], [7.,1.], [8.,1.], [9.,1.], [10.,1.],
[11.,1.], [12.,1.], [13.,1.], [14.,1.],
[0.,2.], [1.,2.], [2.,2.], [3.,2.], [4.,2.],
[5.,2.], [6.,2.], [7.,2.], [8.,2.], [9.,2.], [10.,2.], …
Run Code Online (Sandbox Code Playgroud) 正如这里提到的,scikit-learn 的高斯过程回归 (GPR) 允许“没有先验拟合的预测(基于 GP 先验)”。但是我对我的先验应该是什么有一个想法(即它不应该简单地具有零均值,但也许我的输出,y
,与我的输入,X
,,,线性缩放y = X
)。我如何将这些信息编码到 GPR 中?
下面是一个工作示例,但它假设我之前的均值为零。我读到“GaussianProcessRegressor 不允许指定平均函数,总是假设它是零函数,突出了平均函数在计算后验中的作用减弱。” 我相信这是具有不同尺度的自定义内核(例如异方差)背后的动机X
,尽管我仍在努力更好地了解它们提供的功能。有没有办法绕过零均值先验,以便可以在 scikit-learn 中指定任意先验?
import numpy as np
from matplotlib import pyplot as plt
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import RBF, ConstantKernel as C
def f(x):
"""The function to predict."""
return 1.5*(1. - np.tanh(100.*(x-0.96))) + 1.5*x*(x-0.95) + 0.4 + 1.5*(1.-x)* np.random.random(x.shape)
# Instantiate a Gaussian Process model
kernel = C(10.0, (1e-5, 1e5)) * RBF(10.0, …
Run Code Online (Sandbox Code Playgroud) 我有一个自定义神经网络,我正在对数据进行训练,并试图将网络的输出值限制在两个任意常数之间:[lower_bound,upper_bound]
. 是否有在损失函数中编码此约束的最佳实践?
下面我写了一个最小的工作示例,我在生成的数据上构建和训练神经网络。此外,我[lower_bound,upper_bound] = [-0.5,0.75]
在被优化的损失函数中放置了输出应该介于两者之间的任意约束。但是我尝试使用一种相对粗略的方法来查找预测值超出边界的所有实例,然后简单地使这些项的损失函数成为一个大值(如果预测值在给定的范围内,则为零):
lower_bound = -0.5 #a guessed a priori lower bound on the output
upper_bound = 0.75 #a guessed a priori upper bound on the output
cond_v1_1 = tf.greater(self.v1_pred[:,0], upper_bound*tf.ones(tf.shape(self.v1_pred[:,0])))
cond_v1_2 = tf.greater(-1.0*self.v1_pred[:,0], lower_bound*tf.ones(tf.shape(self.v1_pred[:,0])))
self.red_v1 = tf.where(cond_v1_1, 100000.0*tf.ones(tf.shape(self.v1_pred[:,0])), 0.0*tf.zeros(tf.shape(self.v1_pred[:,0])))
self.red_v1 = tf.where(cond_v1_2, 100000.0*tf.ones(tf.shape(self.v1_pred[:,0])), self.red_v1)
self.loss_cond = tf.reduce_sum(1.0*tf.square(self.red_v1))
Run Code Online (Sandbox Code Playgroud)
但是在训练神经网络时,是否有任何方法或损失函数可以更好地编码这种约束?也许优化器更容易处理和/或修改我的代码本身的更平滑的损失函数?任何关于惩罚/训练下面代码中神经网络的最佳实践的评论和进一步的想法都将不胜感激。
import numpy as np
import tensorflow as tf
end_it = 1000 #number of iterations
frac_train = 1.0 #randomly sampled fraction of data to create training …
Run Code Online (Sandbox Code Playgroud) 我有一本字典,标有的条目{(k,i): value, ...}
。我现在想将此字典转换为2d数组,其中该位置的数组元素[k,i]
的值是带有label的字典中的值(k,i)
。行的长度不必一定是相同的大小(例如,行k = 4
可能上升到索引,i = 60
而行k = 24
可能上升到index i = 31
)。由于不对称,可以使特定行中的所有其他条目都等于0,以便具有矩形矩阵。
当使用来自 scikit-learn 的分类器解决多类问题时,是否有必要使用一种热编码对标签进行编码?例如,我有 3 个类并简单地将它们标记为0
, 1
, 并2
在将这些数据输入不同的分类器进行训练时。据我所知,它似乎工作正常。但是有什么理由不推荐这种基本编码吗?
一些算法,如随机森林,本机处理分类值。对于逻辑回归、多层感知器、高斯朴素贝叶斯和随机森林等方法,如果我没记错的话,这些方法似乎可以在本机处理分类值。这样的评价正确吗?scikit-learn 的哪些分类器本身不处理这些输入并受序数的影响?
使用以下代码,它将标签放置在颜色栏的顶部,但也将y标签放置在颜色栏的顶部。理想情况下,我希望在颜色栏的顶部有一个标签,而在右边则是一个标签(如此处显示的“联系人数量”所示)。
import matplotlib.pyplot as plt
import numpy as np
number = 100
number_ticks = 9
x_input = np.array([np.linspace(-1.,3.,100),]*number)
y_input = np.array([np.arange(0.,100.,1.),]*number).transpose()
output = np.array([np.linspace(0.,1.0,100),]*number)
bounds=np.linspace(0.0,1.0,number_ticks)
cntr1 = plt.contourf(x_input, y_input, output, vmin = 0.0, vmax = 1.0, levels = bounds)
cbar1 = plt.colorbar()
cbar1.set_label(r"$\bf{title}$", labelpad=-40, y=1.03, rotation=270)
cbar1.ax.set_ylabel(r"$\bf{y_label}$", labelpad=-40, y=1.03, rotation=0)
plt.show()
Run Code Online (Sandbox Code Playgroud)
所需的输出既是颜色栏上方的标签,也是其右侧的标签。上面的代码仅将两个标签放置在相同的位置(即上方)。y标签不会像我想要的那样移到颜色栏的一侧(就像在我共享的链接中一样)。
是否有一种有效的方法可以通过让用户在他们想要突破的特定时间(例如按键)简单地提供输入来打破嵌套循环?我在循环中有条件语句,我可以用它来打破循环,但是如果我只是想在任何时候停止循环但仍然希望其余的代码运行,有没有一种简单的方法来做到这一点?为了澄清动机,我想做一些事情:
for x in xrange(1000):
for y in xrange(1000):
for z in xrange(1000):
print x,y,z #function of loop
if (user_has_pressed_key) == True: #a user input that
#can come at any time
#but there should NOT be
#a prompt needed for each iteration to run
break
else:
continue
break
else:
continue
break
Run Code Online (Sandbox Code Playgroud)
我考虑过使用原始输入,但不希望循环等待用户的每次迭代,因为会有很多次迭代.在使用不同的软件包时,似乎有一些建议的解决方案,但即使这些也似乎只是Windows特定的.我在多台计算机上运行此代码,因此理想情况下希望它在不同的操作系统上运行.
我在两个独立的设备\xe2\x80\x94MacBook Air 2013年中(笔记本电脑1)和ThinkPad X1 Yoga 3G(笔记本电脑2)\xe2\x80\x94上运行Python,并在两者上创建numpy数组。尽管两台笔记本电脑的内存相对相似:
\n\n笔记本电脑 1:内存 4 GB 1600 MHz DDR3
\n\n笔记本电脑 2:已安装 RAM 16.0 GB(15.8 GB 可用)
\n\n在观察MemoryError
. 例如:
笔记本电脑1
\n\n>>> import numpy as np\n>>> np.zeros(int(5.*10.**12))\narray([0., 0., 0., ..., 0., 0., 0.])\n>>> np.zeros(int(6.*10.**12))\nPython(6138,0x7fffda9413c0) malloc: *** mach_vm_map(size=48000000000000) failed (error code=3)\n*** error: can\'t allocate region\n*** set a breakpoint in malloc_error_break to debug\nTraceback (most recent call last):\n File "<stdin>", line 1, in <module>\nMemoryError\n
Run Code Online (Sandbox Code Playgroud)\n\n笔记本电脑2
\n\n>>> import numpy as np\n>>> np.zeros(int(1.*10.**8.))\narray([0., 0., …
Run Code Online (Sandbox Code Playgroud) python ×7
scikit-learn ×3
numpy ×2
arrays ×1
colorbar ×1
constraints ×1
dictionary ×1
labels ×1
matplotlib ×1
memory ×1
nested-loops ×1
python-2.7 ×1
regression ×1
tensorflow ×1