我正在尝试在我的主域(http://foo.com)和我的API(http://api.foo.com)之间开发请求.
为了绕过关于跨子域内容的限制,我在主页(http ////foo.com/main.html)上使用iframe,指向iframe.html页面:scripts.api.foo.com.
(scripts.api.foo.com和foo.com在同一台服务器上,api.foo.com在其他服务器上)
> iframe.html:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Iframe</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript">
document.domain = 'foo.com';
function testIframe()
{
$.ajax({
url: "http://api.foo.com/utctime",
timeout: 7000,
complete: function(jqXHR, textStatus){
parent.alert(jqXHR.status);}
});
}
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
> main.html:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript">
document.domain = …
Run Code Online (Sandbox Code Playgroud) 我试图在Tensorflow中创建一个近似正弦函数的神经网络.我已经找到了一些通用函数逼近器的例子,但是我并没有完全理解代码,因为我对Tensorflow很新,我想自己编写代码来理解每一步.
这是我的代码:
import tensorflow as tf
import numpy as np
import math, random
import matplotlib.pyplot as plt
# Create the arrays x and y that contains the inputs and the outputs of the function to approximate
x = np.arange(0, 2*np.pi, 2*np.pi/1000).reshape((1000,1))
y = np.sin(x)
# plt.plot(x,y)
# plt.show()
# Define the number of nodes
n_nodes_hl1 = 100
n_nodes_hl2 = 100
# Define the number of outputs and the learn rate
n_classes = 1
learn_rate = 0.1
# Define input / …
Run Code Online (Sandbox Code Playgroud) python mathematical-optimization approximation neural-network tensorflow
在pytorch,鉴于张量a
的形状(1X11)
和 b
造型(1X11)
,torch.stack((a,b),0)
都会给我形状的张量(2X11)
但是,当a
形状(2X11)
和b
形状时(1X11)
,torch.stack((a,b),0)
会引起错误cf. "两个张量大小必须完全相同".
因为两个张量是模型的输出(包括渐变),我无法将它们转换为numpy使用np.stack()
或np.vstack()
.
是否有任何可能的解决方案,至少GPU内存使用?
我一直在使用Pytorch 0.4.0逻辑回归模型,在我的输入为高维和我的输出必须是一个标量- 0
,1
或2
。
我使用一个线性层与一个 softmax 层相结合来返回一个n x 3
张量,其中每一列表示输入落在三个类 ( 0
,1
或2
)之一中的概率。
但是,我必须返回一个n x 1
张量,因此我需要以某种方式为每个输入选择最高概率并创建一个张量,指示哪个类别的概率最高。如何使用 Pytorch 实现这一目标?
为了说明,我的 Softmax 输出如下:
[[0.2, 0.1, 0.7],
[0.6, 0.2, 0.2],
[0.1, 0.8, 0.1]]
Run Code Online (Sandbox Code Playgroud)
我必须返回这个:
[[2],
[0],
[1]]
Run Code Online (Sandbox Code Playgroud) 张量流中是否有logit函数,即sigmoid函数的逆函数?我已经搜索过谷歌,但没有找到任何。
machine-learning inverse neural-network tensorflow activation-function
我正在尝试实现自定义损失函数并遇到了这个问题。自定义损失函数将如下所示:
def customLoss(z):
y_pred = z[0]
y_true = z[1]
features = z[2]
...
return loss
Run Code Online (Sandbox Code Playgroud)
在我的情况,y_pred
并且y_true
实际上是灰度图像。中z[2]
包含的功能由(x,y)
我想要比较的一对位置y_pred
和y_true
. 这些位置取决于输入训练样本,因此在定义模型时,它们作为输入传递。所以我的问题是:我如何使用张量features
来索引张量y_pred
和y_true
?
我正在关注tensorflow 的本教程:
我试图理解tf.session.run()
. 我知道您必须在会话中的图形中运行操作。
是train_step
的,因为它封装了这个特殊的例子在网络的所有操作通过呢?我试图理解为什么我不需要将任何其他变量传递给会话,例如cross_entropy
.
sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
Run Code Online (Sandbox Code Playgroud)
这是完整的代码:
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
import tensorflow as tf
x = tf.placeholder(tf.float32, [None, 784])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
y = tf.nn.softmax(tf.matmul(x, W) + b)
y_ = tf.placeholder(tf.float32, [None, 10])
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
sess = tf.InteractiveSession()
tf.global_variables_initializer().run()
for _ in range(10):
batch_xs, batch_ys = mnist.train.next_batch(100)
sess.run(train_step, feed_dict={x: batch_xs, …
Run Code Online (Sandbox Code Playgroud) 该文档含糊不清,没有示例代码向您展示如何使用它。它的文档是
将参数组添加到Optimizer的param_groups中。
当对预训练的网络进行微调时,这很有用,因为可以使冻结的层成为可训练的,并随着训练的进行而添加到优化器中。
参数:param_group(dict)–指定应优化哪些张量以及组优化选项。(具体)–
我假设我可以param_group
通过输入从模型的state_dict()
?获得的值来获取参数。例如所有实际重量值?我之所以这样问是因为我想建立一个渐进网络,这意味着我需要不断地从新创建的卷积和激活模块中输入Adam参数。
当不使用 KL 散度项时,VAE 几乎完美地重建了 mnist 图像,但在提供随机噪声时无法正确生成新图像。
当使用 KL 散度项时,VAE 在重建和生成图像时给出相同的奇怪输出。
这是损失函数的pytorch代码:
def loss_function(recon_x, x, mu, logvar):
BCE = F.binary_cross_entropy(recon_x, x.view(-1, 784), size_average=True)
KLD = -0.5 * torch.sum(1 + logvar - mu.pow(2) - logvar.exp())
return (BCE+KLD)
Run Code Online (Sandbox Code Playgroud)
recon_x 是重建图像,x 是 original_image,mu 是均值向量,而 logvar 是包含方差对数的向量。
这里出了什么问题?提前致谢 :)
bayesian-networks autoencoder deep-learning pytorch loss-function
如问题所述,-1
在pytorch 中做什么view
?
In [2]: a = torch.arange(1, 17)
In [3]: a
Out[3]:
tensor([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.,
11., 12., 13., 14., 15., 16.])
In [7]: a.view(-1,1)
Out[7]:
tensor([[ 1.],
[ 2.],
[ 3.],
[ 4.],
[ 5.],
[ 6.],
[ 7.],
[ 8.],
[ 9.],
[ 10.],
[ 11.],
[ 12.],
[ 13.],
[ 14.],
[ 15.],
[ 16.]])
In [8]: a.view(1,-1)
Out[8]:
tensor([[ 1., 2., 3., 4., 5., 6., …
Run Code Online (Sandbox Code Playgroud) pytorch ×5
python ×4
tensorflow ×4
python-3.x ×2
reshape ×2
ajax ×1
autoencoder ×1
cross-domain ×1
iframe ×1
inverse ×1
jquery ×1
keras ×1
numpy ×1
parameters ×1
slice ×1
softmax ×1
subdomain ×1
tensor ×1