“人造太阳”计划

机器学习算法------梯度下降法

倾然丶 夕夏残阳落幕 提交于 2019-12-03 11:55:38
优秀的讲解博客 刘建平的博客 算法简述 梯度下降通常是通过迭代的方式来搜索某个函数的极大/小值,他对目标函数每个变量求偏导得出梯度,也就是沿着梯度方向函数值会增加的最快,那么要求最小值就要沿着梯度值的反方向,梯度下降分为随机梯度下降与批量梯度下降,以及小批量梯度下降,随机梯度相比批量梯度耗时少,但精度不如批量高,批量每一步都沿着下降最快的方向下降,但是样本很多的话 耗时很多,还有就是随机梯度具有随机的特性,可能会跳出局部最优解从而到达全局最优解,而批量梯度则会一步步的走向局部最优解 模拟梯度下降法 梯度下降搜索一元二次方程最小值 通过梯度下降 求解 y = (x-2.5) ^ 2 - 1的 最小值 import numpy as np import matplotlib.pyplot as plt plot_x = np.linspace(- 1. , 6. , 141 ) # 造一个数据 plot_y = (plot_x- 2.5 ) ** 2 - 1. #plt.plot(plot_x, plot_y) #plt.show() epsilon = 1e-8 #误差 eta = 0.1 # 学习率 def J (theta) : # 要求解的函数 return (theta - 2.5 ) ** 2 - 1. def dJ (theta) : # 对函数求导之后的式子

How can multiple threads share an iterator?

匿名 (未验证) 提交于 2019-12-03 09:14:57
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I've been working on a function that will copy a bunch of files from a source to a destination using Rust and threads. I'm getting some trouble making the threads share the iterator. I am not still used to the borrowing system: extern crate libc; extern crate num_cpus; use libc::{c_char, size_t}; use std::thread; use std::fs::copy; fn python_str_array_2_str_vec<T, U, V>(_: T, _: U) -> V { unimplemented!() } #[no_mangle] pub extern "C" fn copyFiles( sources: *const *const c_char, destinies: *const *const c_char, array_len: size_t, ) { let src

class with __iter__ assigned in constructor not recognized as iterator

匿名 (未验证) 提交于 2019-12-03 09:06:55
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Kind of a weird question about iterators. While investigating a different question, I found the following. Here is an iterable that works: class CacheGen(object): def __init__(self, iterable): if isinstance(iterable, (list, tuple, dict)): self._myiter = iterable else: self._myiter = list(iterable) def __iter__(self): return self._myiter.__iter__() def __contains__(self, key): return self._myiter.__contains__(key) def __getitem__(self, key): return self._myiter.__getitem__(key) Here is a similar iterable that doesn't: class CacheGen2(object):

Building a C iterator macro with a pre-C99 compiler

匿名 (未验证) 提交于 2019-12-03 08:59:04
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: My code, which must be compiled with a pre-C99 compiler (we're working on updating but it's an enormous task), is calling into a utility library designed with C99 in mind. In particular, these utilities define a hashmap type and provide a macro to iterate through it similar to the following: #define MAP_FOREACH(key, val, map) \ for (struct _map_iterator iter __attribute__((cleanup(_map_iter_cleanup))); \ (key) = iter->pair->key, \ (value) = iter->pair->value; \ iter = iter->get_next_cb()) The actual code has a bit more to it (functionality

reading rows of big csv file in python

匿名 (未验证) 提交于 2019-12-03 08:52:47
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have a very big csv file which I cannot load in memory in full. So I want to read it piece by piece, convert it into numpy array and then do some more processing. I already checked: Lazy Method for Reading Big File in Python? But problem here is that it is a normal reader, and I am unable to find any option of specifying size in csvReader. Also since I want to convert rows into numpy array, i dont want to read any line in half, so rather than specifying size, I want something where I can specify "no of rows" in reader. Is there any built

How to deal with ellipsis (…) in the presence of optional arguments?

匿名 (未验证) 提交于 2019-12-03 08:48:34
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have a problem with ellipsis when I use optional arguments in my function definition. To clarify, I define following functions: func1 <- function (x) (x-2)^2 func3 <- function (fun, arg.curve.user){ arg.curve.user$expr <- substitute(func1) arg.curve.default <- list(col = "blue", n = 1000, main = "This is a test") arg.curve <- modifyList (arg.curve.default, arg.curve.user) do.call("curve", arg.curve) } # optimizes func1 and call func2 to plot func1 func2 <- function (lb, ub, n.restarts = 5, n.sim = 10, ...){ arg.curve.user <- as.list

CUDA thrust zip_iterator tuple transform_reduce

匿名 (未验证) 提交于 2019-12-03 08:48:34
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I want to compute for vectors and , where denotes the magnitude of the vector . Since this involves taking the square root of the sum of the squares of the differences between each corresponding component of the two vectors, it should be a highly parallelizable task. I am using Cuda and Thrust, through Cygwin, on Windows 10. Both Cuda and Thrust are in general working. The below code compiles and runs (with nvcc), but only because I have commented out three lines toward the bottom of main , each of which I think should work but does not.

How to have list() consume __iter__ without calling __len__?

匿名 (未验证) 提交于 2019-12-03 08:35:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I have a class with both an __iter__ and a __len__ methods. The latter uses the former to count all elements. It works like the following: class A : def __iter__ ( self ): print ( "iter" ) for _ in range ( 5 ): yield "something" def __len__ ( self ): print ( "len" ) n = 0 for _ in self : n += 1 return n Now if we take e.g. the length of an instance it prints len and iter , as expected: >>> len ( A ()) len iter 5 But if we call list() it calls both __iter__ and __len__ : >>> list ( A ()) len iter iter [ 'something' , 'something' ,

Python中的迭代器与生成器

时光怂恿深爱的人放手 提交于 2019-12-03 03:30:42
Python中的迭代器与生成器介绍 一、迭代器iterator 迭代器是访问可迭代对象的工具。 迭代器是指用iter(obj)函数返回的对象 迭代器是可以用next(it)函数获取可迭代对象的数据 1、迭代器函数 iter()与next() iter(iterable)从可迭代对象中返回一个迭代器,iterable必须是 能提供一个迭代器的对象 next(iterable) 从迭代器iterator中获取下一个记录,如果无法获 取下一条记录,则触发StopIterator异常 说明: ①迭代器只能往前取值,不会后退 ②用iter函数可以返回一个可迭代对象的迭代器 示例: >>> L = [1,3,5,7] >>> it = iter(L) #让L提供一个能访问自己的迭代器 >>> next(it) 1 >>> next(it) 3 2、迭代器的用途: 可以依次访问可迭代对象的数据(可代替循环遍历) 示例:>>> L = [1,23,45,6] >>> it = iter(L) >>> while True: try: x = next(it) print(x) except StopIteration: break 二、迭代工具函数: 作用是生成一个个性化的可迭代对象 函数: zip(iter1 [,iter2[....]]) 返回一个zip对象,此对象用于生成元组

Python 之迭代器与生成器

淺唱寂寞╮ 提交于 2019-12-03 03:29:57
目录 一 , 迭代器 1 , 初识迭代器 2 , 创建一个迭代器 3 , StopIteration 二 , 生成器 1 , 初识生成器 一 , 迭代器 1 , 初识迭代器 迭代是Python最强大的功能之一,是访问集合元素的一种方式。 迭代器是一个可以记住遍历的位置的对象。 迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束。迭代器只能往前不会后退。 迭代器有两个基本的方法: iter() 和 next() 。 字符串,列表或元组对象都可用于创建迭代器: >>>list=[1,2,3,4] >>> it = iter(list) # 创建迭代器对象 >>> print (next(it)) # 输出迭代器的下一个元素 1 >>> print (next(it)) 2 >>> 迭代器对象可以使用常规for语句进行遍历: #!/usr/bin/python3 list=[1,2,3,4] it = iter(list) # 创建迭代器对象 for x in it: print (x, end=" ") 执行以上程序,输出结果如下: 1 2 3 4 也可以使用 next() 函数: #!/usr/bin/python3 import sys # 引入 sys 模块 list=[1,2,3,4] it = iter(list) # 创建迭代器对象 while True: