假设我有一个非常简单的神经网络,如多层感知器.对于每一层,激活功能是S形的,并且网络是完全连接的.
在TensorFlow中,这可能是这样定义的:
sess = tf.InteractiveSession()
# Training Tensor
x = tf.placeholder(tf.float32, shape = [None, n_fft])
# Label Tensor
y_ = tf.placeholder(tf.float32, shape = [None, n_fft])
# Declaring variable buffer for weights W and bias b
# Layer structure [n_fft, n_fft, n_fft, n_fft]
# Input -> Layer 1
struct_w = [n_fft, n_fft]
struct_b = [n_fft]
W1 = weight_variable(struct_w, 'W1')
b1 = bias_variable(struct_b, 'b1')
h1 = tf.nn.sigmoid(tf.matmul(x, W1) + b1)
# Layer1 -> Layer 2
W2 = weight_variable(struct_w, 'W2')
b2 = bias_variable(struct_b, …Run Code Online (Sandbox Code Playgroud) 假设我有一个C库API函数,它将指针指针作为参数.但是由于我使用C++编程,我想利用std向量来处理动态内存.如何有效地将矢量矢量转换为指针指针?现在我正在使用它.
#include <vector>
/* C like api */
void foo(short **psPtr, const int x, const int y);
int main()
{
const int x = 2, y = 3;
std::vector<std::vector<short>> vvsVec(x, std::vector<short>(y, 0));
short **psPtr = new short*[x];
/* point psPtr to the vector */
int c = 0;
for (auto &vsVec : vvsVec)
psPtr[c++] = &vsVec[0];
/* api call */
foo(psPtr, x, y);
delete[] psPtr;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是实现目标的最佳方式吗?在这种情况下,我可以通过使用迭代器或某些std方法来摆脱"新删除"的事情吗?提前致谢.
编辑: 根据答案我现在使用此版本与C代码接口.我在这里发帖.
#include <vector>
/* C like api */
void foo(short …Run Code Online (Sandbox Code Playgroud) 我正在尝试实施维纳滤波器以对模糊图像执行反卷积.我的实现是这样的
import numpy as np
from numpy.fft import fft2, ifft2
def wiener_filter(img, kernel, K = 10):
dummy = np.copy(img)
kernel = np.pad(kernel, [(0, dummy.shape[0] - kernel.shape[0]), (0, dummy.shape[1] - kernel.shape[1])], 'constant')
# Fourier Transform
dummy = fft2(dummy)
kernel = fft2(kernel)
kernel = np.conj(kernel) / (np.abs(kernel) ** 2 + K)
dummy = dummy * kernel
dummy = np.abs(ifft2(dummy))
return np.uint8(dummy)
Run Code Online (Sandbox Code Playgroud)
此实现基于Wiki页面.
使用的TIFF图像来自:http://www.ece.rice.edu/~wakin/images/lena512color.tiff
但这里有一个PNG版本:

我有一个由对角内核模糊的输入图像运动,并添加了一些高斯加性噪声.lena图片为512x512,模糊内核为11x11.
当我将wiener_filter应用于此图像时,结果就像这样.
.
我认为这种模糊的图像质量不高.所以我想问一下我的实现是否正确.
非常感谢你!
更新我添加噪音的方式.
from scipy.signal import gaussian, convolve2d
def blur(img, …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用python实现JPEG压缩.当我尝试对tiff图像应用DCT,量化,IDCT过程时,我发现scipy.fftpack.dct/idct有些奇怪.
由于scipy包中只有1D dct/idct,所以我正在为2D dct做这个
import numpy as np
from scipy.fftpack import dct, idct
def dct2(block):
return dct(dct(block.T).T)
def idct2(block):
return idct(idct(block.T).T)
Run Code Online (Sandbox Code Playgroud)
我使用简单的3x3矩阵测试了2D dct/idct.我希望在这个测试用例中得到一个True矩阵.
a = np.random.randint(0,255,9).reshape(3,3)
print a == idct2(dct2(a))
Run Code Online (Sandbox Code Playgroud)
然而事实证明,在idct2(dct2(a))之后,结果通过与原始矩阵相比的常数因子来缩放.
我想问一下是否有办法实现一组2D dct/idct,这样在idct(dct(a))操作之后我可以获得与输入相同的输出.
在Linux Ubuntu中,我可以使用动态库编译C++源代码,并通过在编译前设置环境变量$LD_RUN_PATH来告诉gcc编译器在哪里找到.so lib文件。我想知道 OSX 中的 clang 编译器是否有等效的方法来执行此类运行时动态库路径搜索?
非常感谢!
编辑:
下面给出的重复链接不是我想要的!
我想将 dylib 的搜索路径编译到可执行文件中。在运行时,不应为$DYLD_LIBRARY_PATH环境路径显式设置任何路径信息。也就是说,当您 echo $DYLD_LIBRARY_PATH时,不应看到指向 dylib 的路径。
我通过使用实现了这一点
otool -L <executable>
Run Code Online (Sandbox Code Playgroud)
读取我的可执行文件中的搜索路径。
对于任何错误或缺少搜索路径的情况,我使用 install_name_too 来更新或添加搜索路径信息。
对我来说,我正在改变我原来的链接路径。我用
install_name_tool -change <old_path> <new_path> <executable>
Run Code Online (Sandbox Code Playgroud)
用于链接路径设置。之后,我可以正确运行链接到 dylib 的可执行文件,而无需在$LD_LIBRARY_PATH或$LD_FALLBACK_LIBRARY_PATH中设置任何内容。
我有一个基于TensorFlow的神经网络和一组变量.
培训功能如下:
def train(load = True, step)
"""
Defining the neural network is skipped here
"""
train_step = tf.train.AdamOptimizer(1e-4).minimize(mse)
# Saver
saver = tf.train.Saver()
if not load:
# Initalizing variables
sess.run(tf.initialize_all_variables())
else:
saver.restore(sess, 'Variables/map.ckpt')
print 'Model Restored!'
# Perform stochastic gradient descent
for i in xrange(step):
train_step.run(feed_dict = {x: train, y_: label})
# Save model
save_path = saver.save(sess, 'Variables/map.ckpt')
print 'Model saved in file: ', save_path
print 'Training Done!'
Run Code Online (Sandbox Code Playgroud)
我正在调用这样的训练函数:
# First train
train(False, 1)
# Following train
for i …Run Code Online (Sandbox Code Playgroud) 我有一个MatrixXf变量和一个VectorXf变量.我想使用我的矩阵上的Vector执行行划分.可以这样做吗?
#include <iostream>
#include "Eigen/Dense"
using namespace std;
using namespace Eigen;
int main() {
MatrixXf mat(3, 2);
mat << 1, 2,
3, 4,
5, 6;
VectorXf vec(2);
vec << 2, 3;
mat = mat.rowwise() / vec;
cout << mat << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我期望得到一个有价值的矩阵[0.5,0.667; 1.5,1.333; 2.5,2].
非常感谢你!
我正在 Windows 上使用 Visual Studio 编译器(特别是 Visual Studio 2015 Update 3)进行 C++ 开发。
对于一些 DSP 相关工作,我使用unsigned int / unsigned long数据类型。我想知道这两种内置 C/C++ 类型有什么区别。
我通过 Google 和 SO 进行了一些搜索,找到了这些参考资料。
我假设 cppreference 文档是 ISO C++11 标准的摘要。因此,从“标准”来看,unsigned和unsigned int是 16/32 位,具体取决于 LP/ILP 32/64 数据模型,而unsigned long和unsigned long int是 32/64 位,具体取决于 LP/ILP 32/64 数据模型。
对于 MSDN 和 …
在c ++ 17中,我们有std :: any,它在内存中存储变量类型的对象.好的部分是我可以创建一个std :: any的向量来模拟任意类型对象的容器.
每当从容器一个查询返回的对象将使用 的std :: any_cast打电话时使用完全相同的类型的std :: make_any创建任何对象.以下是我如何实现这一目标的片段
#include <any>
#include <iostream>
#include <unordered_map>
#include <vector>
#include <set>
int main()
{
/* create some objects */
std::set<int> mySet = { 1, 2, 3 };
std::vector<int> myVec = { 3, 4, 5 };
std::unordered_map<int, std::vector<int>> myHash = { std::make_pair(1, myVec) };
/* create any store */
std::vector<std::any> anyStore;
anyStore.push_back(std::make_any<decltype(mySet)>(mySet));
anyStore.push_back(std::make_any<decltype(myVec)>(myVec));
anyStore.push_back(std::make_any<decltype(myHash)>(myHash));
/* get object back */
auto newSet = …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用python进行JPEG压缩.我加载一个tiff图像并将其存储为numpy uint8 RGB数组.我这样做是为了颜色映射.
def rgb2ycbcr(im):
cbcr = np.empty_like(im)
r = im[:,:,0]
g = im[:,:,1]
b = im[:,:,2]
# Y
cbcr[:,:,0] = .299 * r + .587 * g + .114 * b
# Cb
cbcr[:,:,1] = 128 - .169 * r - .331 * g + .5 * b
# Cr
cbcr[:,:,2] = 128 + .5 * r - .419 * g - .081 * b
return np.uint8(cbcr)
def ycbcr2rgb(im):
rgb = np.empty_like(im)
y = im[:,:,0]
cb = im[:,:,1] - …Run Code Online (Sandbox Code Playgroud) c++ ×5
python ×4
tensorflow ×2
c++11 ×1
c++17 ×1
compilation ×1
convolution ×1
dct ×1
eigen ×1
g++ ×1
gcc ×1
image ×1
io ×1
jpeg ×1
linux ×1
macos ×1
motion-blur ×1
pointers ×1
scipy ×1
stdvector ×1