我注意到SQLAlchemy缓慢获取(和ORMing)一些数据,使用裸骨SQL获取相当快.首先,我创建了一个包含一百万条记录的数据库:
mysql> use foo
mysql> describe Foo;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| A | int(11) | NO | | NULL | |
| B | int(11) | NO | | NULL | |
| C | int(11) | NO | | NULL | |
+-------+---------+------+-----+---------+-------+
mysql> SELECT COUNT(*) FROM Foo;
+----------+
| COUNT(*) |
+----------+
| 1000000 …Run Code Online (Sandbox Code Playgroud) 深度学习已成功应用于几个大型数据集,用于分类少数类(猫,狗,汽车,飞机等),其性能优于简单的描述符,例如SIFT上的特征包,颜色直方图等.
然而,培训这样的网络需要每个班级有大量的数据和大量的培训时间.然而,在花费时间设计和训练这样的设备并收集训练数据之前,通常一个人没有足够的数据或只是想知道卷积神经网络可能做得多好.
在这种特殊情况下,使用现有技术出版物使用的某些基准数据集来配置和训练网络可能是理想的,并且只需将其应用于您可能作为特征提取器的某些数据集.
这导致每个图像的一组特征,其可以馈送到经典分类方法,如SVM,逻辑回归,神经网络等.
特别是当一个人没有足够的数据来训练CNN时,我可以预期这会超过CNN在少数样本上训练的管道.
我正在查看tensorflow教程,但他们似乎总是有一个明确的培训/测试阶段.我找不到带有预先配置的CNN特征提取器的pickle文件(或类似文件).
我的问题是:这些预先训练好的网络是否存在,我在哪里可以找到它们.另外:这种方法有意义吗?我在哪里可以找到CNN +权重?
编辑
WRT @约翰的评论我试着用'DecodeJpeg:0'和'DecodeJpeg/contents:0',并检查了输出,这是不同的(:S)
import cv2, requests, numpy
import tensorflow.python.platform
import tensorflow as tf
response = requests.get('https://i.stack.imgur.com/LIW6C.jpg?s=328&g=1')
data = numpy.asarray(bytearray(response.content), dtype=np.uint8)
image = cv2.imdecode(data,-1)
compression_worked, jpeg_data = cv2.imencode('.jpeg', image)
if not compression_worked:
raise Exception("Failure when compressing image to jpeg format in opencv library")
jpeg_data = jpeg_data.tostring()
with open('./deep_learning_models/inception-v3/classify_image_graph_def.pb', 'rb') as graph_file:
graph_def = tf.GraphDef()
graph_def.ParseFromString(graph_file.read())
tf.import_graph_def(graph_def, name='')
with tf.Session() as sess:
softmax_tensor = sess.graph.get_tensor_by_name('pool_3:0')
arr0 = numpy.squeeze(sess.run(
softmax_tensor,
{'DecodeJpeg:0': …Run Code Online (Sandbox Code Playgroud) 根据着名的check_blas.py脚本,我写了一个来检查theano实际上可以使用多个核心:
import os
os.environ['MKL_NUM_THREADS'] = '8'
os.environ['GOTO_NUM_THREADS'] = '8'
os.environ['OMP_NUM_THREADS'] = '8'
os.environ['THEANO_FLAGS'] = 'device=cpu,blas.ldflags=-lblas -lgfortran'
import numpy
import theano
import theano.tensor as T
M=2000
N=2000
K=2000
iters=100
order='C'
a = theano.shared(numpy.ones((M, N), dtype=theano.config.floatX, order=order))
b = theano.shared(numpy.ones((N, K), dtype=theano.config.floatX, order=order))
c = theano.shared(numpy.ones((M, K), dtype=theano.config.floatX, order=order))
f = theano.function([], updates=[(c, 0.4 * c + .8 * T.dot(a, b))])
for i in range(iters):
f(y)
Run Code Online (Sandbox Code Playgroud)
运行此操作python3 check_theano.py表明正在使用8个线程.更重要的是,代码的运行速度比没有os.environ设置快9倍,只需1个核心:7.863s vs 71.292s.
所以,我希望Keras现在在调用时fit(或者predict就此而言)也使用多个内核.但是,以下代码不是这种情况:
import …Run Code Online (Sandbox Code Playgroud) 通常,当我编程时,我使用多态,因为它自然地模拟我需要的对象.另一方面,我经常使用标准容器来存储这些对象,我倾向于避免使用指针,因为这要求我释放对象而不是将它们从堆栈中弹出或要求我确定对象将保持不变我使用指针时的堆栈.当然有各种各样的指针容器对象可以为你完成这项任务,但根据我的经验,它们也不理想甚至烦人.那是; 如果存在这样一个简单的解决方案,它本来就是用c ++语言,对吧?;)
所以让我们有一个经典的例子:
#include <iostream>
#include <vector>
struct foo {};
struct goo : public foo {};
struct moo : public foo {};
int main() {
std::vector<foo> foos;
foos.push_back(moo());
foos.push_back(goo());
foos.push_back(goo());
foos.push_back(moo());
return 0;
}
Run Code Online (Sandbox Code Playgroud)
请参阅:http://ideone.com/aEVoSi.这工作正常,如果对象具有不同的sizeof,编译器可能会应用切片.但是,由于c ++不知道像Java这样的实例,并且据我所知,没有足够的替代存在,在从向量中获取它们作为foo之后,无法访问继承类的属性.
因此,人们将使用虚函数,但是这不允许一个人分配foo,因此不允许在向量中使用它们.请参阅为什么我们不能声明std :: vector <AbstractClass>?.
例如,我可能希望能够打印两个子类,简单的功能,对吗?
#include <iostream>
#include <vector>
struct foo {
virtual void print() =0;
virtual ~foo() {}
};
struct goo : public foo {
int a;
void print() { std::cout << "goo"; }
};
struct …Run Code Online (Sandbox Code Playgroud) 关于张量流网站上的MNIST教程,我进行了一项实验(要点),看看不同权重初始化对学习的影响.我注意到,根据我在流行的[Xavier,Glorot 2010]论文中所读到的内容,无论重量初始化如何,学习都很好.
不同的曲线表示w用于初始化卷积和完全连接的层的权重的不同值.请注意,所有值w做工精细,即使0.3和1.0以更低的性能结束和一些价值观培养更快-尤其是0.03和0.1是最快的.然而,该图显示了相当大范围的w工作,表明重量初始化的"稳健性".
def weight_variable(shape, w=0.1):
initial = tf.truncated_normal(shape, stddev=w)
return tf.Variable(initial)
def bias_variable(shape, w=0.1):
initial = tf.constant(w, shape=shape)
return tf.Variable(initial)
Run Code Online (Sandbox Code Playgroud)
问题:为什么这个网络不会受到消失或爆炸梯度问题的影响?
我建议你阅读有关实现细节的要点,但这里是代码供参考.我的nvidia 960m花了大约一个小时,虽然我想它也可以在合理的时间内在CPU上运行.
import time
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
from tensorflow.python.client import device_lib
import numpy
import matplotlib.pyplot as pyplot
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
# Weight initialization
def weight_variable(shape, w=0.1):
initial …Run Code Online (Sandbox Code Playgroud) 注意:我没有意识到指针被认为是迭代器,因此有人可能会认为我所谓的缺乏内存地址稳定性应该称为迭代器失效.请阅读副本,以获得更抽象和更健全的问题.
我的问题与这个问题有关:当push_back新元素到std :: vector时,C++引用会发生变化.
我想使用一组对象,为简单起见,这些对象在内存中只存在一次.因此,我想使用一个容器,如std :: vector,来存储所有对象一次.然后我将使用指向其他结构中的对象的指针.不幸的是,std :: vector可能会改变它元素的内存地址,因此使用指向这些元素的指针是不明确的.我需要指针,因为我想使用其他结构来引用这些对象,比如std :: priority_queue或其他std数据结构.
在特定情况下,对象是图算法中连接的标签,这意味着它们是在整个算法中创建的,因此不能预先分配.这意味着std :: vector是不够的,因为它可能会重定位其内容,使指向std :: priority_queues或其他数据结构中可能存在的这些标签的指针无效.
但是,我需要标签的唯一时刻是创建它们的时候或者我可以从包含数据结构以外的数据结构访问它们.因此,我永远不需要从容器中获取第n个元素,我只需要能够将对象保留在堆栈或堆上,并在创建它们时获取指针以在其他结构中使用它.最后,当容器从堆栈中弹出时,其中的元素需要很好地清理.我认为std :: list可能是合适的,因为我对抽象链表的知识永远不需要重新分配; 允许稳定的指针.
但是,我无法找到std :: lists的指针稳定性为何.也许有一些优越的东西,一些容器类完全符合我的要求.当然,我总是可以使用new,将所有指针追加到std :: list并迭代在最后执行删除.但这不是我喜欢的方式,因为它需要更多的内存管理,因为我认为应该只需要获得稳定的指针.
问题: std :: list 指针是否稳定?有比std :: list更好的解决方案吗?
为了说明这个问题,我也做了这个例子:http://ideone.com/OZYeuw.用std :: vector替换std :: list,行为变得不确定.
#include <iostream>
#include <list>
#include <queue>
#include <vector>
struct Foo {
Foo(int _a) : a(_a) {}
int a;
};
struct FooComparator …Run Code Online (Sandbox Code Playgroud) 当然,答案是"不",因为写它的人认为它很难,但我想知道为什么.
考虑到(无模板)类通常在头文件中声明,然后将其包含在几个单独编译的文件中,请考虑这两个文件:
#include <cstddef>
struct Foo {
public:
int pub;
private:
int priv;
};
size_t getsize1(Foo const &foo) {
return sizeof(foo);
}
Run Code Online (Sandbox Code Playgroud)
#include <cstddef>
struct Foo {
public:
int pub;
private:
int priv;
};
size_t getsize2(Foo const &foo) {
return sizeof(foo);
}
Run Code Online (Sandbox Code Playgroud)
通常,Foo将在头文件中声明并包含在两者中,但效果如上所示.(也就是说,包含一个标题不是魔术,它只是将标题内容放在该行上.)我们可以编译它们并将它们链接到以下内容:
#include <iostream>
struct Foo {
public:
int pub;
private:
int priv;
};
size_t getsize1(Foo const &);
size_t getsize2(Foo const &);
int main() {
Foo foo;
std::cout << getsize1(foo) << ", " << getsize2(foo) << …Run Code Online (Sandbox Code Playgroud) 我想移动一个字符串流,在现实世界的应用程序中我有一些stringstream类数据成员,我想在操作期间重用不同的字符串.
stringstream没有复制赋值或复制构造函数,这是有道理的.但是,根据cppreference.com和cplusplus.com,std :: stringstream应该定义一个移动赋值和交换操作.我试过了两个,都失败了.
#include <string> // std::string
#include <iostream> // std::cout
#include <sstream> // std::stringstream
int main () {
std::stringstream stream("1234");
//stream = std::move(std::stringstream("5678"));
stream.operator=(std::move(std::stringstream("5678")));
//stream.operator=(std::stringstream("5678"));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
prog.cpp:11:56: error: use of deleted function ‘std::basic_stringstream<char>& std::basic_stringstream<char>::operator=(const std::basic_stringstream<char>&)’
stream.operator=(std::move(std::stringstream("5678")));
Run Code Online (Sandbox Code Playgroud)
编译器声明所有三个语句都没有复制赋值,这是真的.但是,我不明白为什么它没有使用move-assignment,特别是因为std :: move应该返回一个rvalue引用.Stringstream应该有一个移动分配,如下所示:http://en.cppreference.com/w/cpp/io/basic_stringstream/operator%3D
PS:我正在使用c ++ 11,因此rvalue-references是'world'的一部分.
我发现这很奇怪,我从cplusplus.com复制了示例代码,但失败了:
// swapping stringstream objects
#include <string> // std::string
#include <iostream> // std::cout
#include <sstream> // std::stringstream
int main () {
std::stringstream foo;
std::stringstream bar;
foo …Run Code Online (Sandbox Code Playgroud) 我经常写代码如下:
sorted(some_dict.items(), key=lambda x: x[1])
sorted(list_of_dicts, key=lambda x: x['age'])
map(lambda x: x.name, rows)
Run Code Online (Sandbox Code Playgroud)
在哪里我想写:
sorted(some_dict.items(), key=idx_f(1))
sorted(list_of_dicts, key=idx_f('name'))
map(attr_f('name'), rows)
Run Code Online (Sandbox Code Playgroud)
使用:
def attr_f(field):
return lambda x: getattr(x, field)
def idx_f(field):
return lambda x: x[field]
Run Code Online (Sandbox Code Playgroud)
在python中是否存在像idx_f和attr_f这样的仿函数创建器,使用它们比lambda更清晰吗?
生成一些随机的高斯坐标,我注意到TSP求解器返回了可怕的解,但是对于相同的输入,它又一次又一次地返回了相同的可怕解。
给出以下代码:
import numpy
import math
from ortools.constraint_solver import pywrapcp
from ortools.constraint_solver import routing_enums_pb2
import matplotlib
%matplotlib inline
from matplotlib import pyplot, pylab
pylab.rcParams['figure.figsize'] = 20, 10
n_points = 200
orders = numpy.random.randn(n_points, 2)
coordinates = orders.tolist()
class Distance:
def __init__(self, coords):
self.coords = coords
def distance(self, x, y):
return math.sqrt((x[0] - y[0]) ** 2 + (x[1] - y[1]) ** 2)
def __call__(self, x, y):
return self.distance(self.coords[x], self.coords[y])
distance = Distance(coordinates)
search_parameters = pywrapcp.RoutingModel.DefaultSearchParameters()
search_parameters.first_solution_strategy = (
routing_enums_pb2.FirstSolutionStrategy.LOCAL_CHEAPEST_ARC)
search_parameters.local_search_metaheuristic = …Run Code Online (Sandbox Code Playgroud) c++11 ×4
c++ ×3
python ×3
tensorflow ×2
blas ×1
functor ×1
keras ×1
mnist ×1
mysql ×1
openblas ×1
or-tools ×1
orm ×1
performance ×1
pointers ×1
python-3.4 ×1
python-3.x ×1
sqlalchemy ×1
struct ×1
swap ×1
theano ×1
vector ×1