Big Ben

一个半吊子的编码爱好者

0%

Edit

Angular JS模块化,主要借助于3个方式:directive, component, module

directive & 3 bindings

2-way binding “=”

function MyDirective() {
var ddo = {
scope: {
myProp: '=attributeName'
}
...
};
return ddo;
}
<!-- index.html -->
<my-directive attribute-name="outerProp">
</my-directive>
<!-- directive.html -->
<p>{{my-prop}}</p>

所有对attribute-name或者my-prop的改动,两个html里都会做同步修改

1-way binding “<” and “@”

function MyDirective() {
var ddo = {
scope: {
prop: '<',
},
...
};
return ddo;
}

与上面不同,这里只有index.html才能修改prop。

function MyDirective() {
var ddo = {
scope: {
prop: '@',
},
...
};
return ddo;
}

这里prop就只能绑定index.html里的属性变量,如下

<my-directive my-attribute="{{outerProp}}">
</my-directive>

可见my-attribute里面不能有变量(例如controller或者controller的成员)。而且和’<’类似,’@’也是单向的。

简化版的directive - component

angular.module('App', [])
.component('myComponent', {
templateUrl: 'template.html',
controller: CompController,
bindings: {
prop1: '<',
prop2: '@',
onAction: '&'
}
});

语法基本与directive类似,用bindings取代了scope。

‘bindings’ object is the isolate scope param mapping definition.

isolate scope里面没有双向绑定了,明确了输入输出:

  • ‘<’, ‘@’作为输入
  • ‘&’作为输出,意思是通过用’&’注册callback来将输出送到相应的模块

使用’&’的基本方法

调用component的DOM写法。

<my-component
prop1="val-1"
prop2="@parentProp"
on-action="parentFunction(myArg)">
{{ $ctrl.prop1.prop }} and {{ $ctrl.prop2 }}
</my-component>

template的写法,component的写法如上一节的例子。

<div
ng-click="$ctrl.onAction({myArg: 'val'})">
{{ $ctrl.prop1.prop }} and {{ $ctrl.prop2 }}
</div>

注意:

  • onAction在标签里的写法是on-action
  • myArg在template中是key name。value值可以来自于controller($ctrl),亦或bindings中的’<’, ‘@’,方法如下,通过inject controller。
angular.module('MenuApp')
.controller('ItemListController', ItemListController)
ItemListController.$inject = ['items']
function ItemListController(items, currentItem) {
var list = this
list.items = items
}

Components Lifecycle

  • $onInit – controller initialization code
  • $onChanges(changeObj) – called whenever one-way bindings are updated
    • changeObj.currentValue, changeObj.previousValue
  • $postLink – similar to ‘link’ in directive
  • $onDestroy – when scope is about to be destroyed

Link for directive

  • DOM manipulation is usually done inside of the link function
    • Declared on the DDO
  • Link function does not support injection
    • To use injected components, services, inject them into directive
  • ‘scope’ parameter is the exact $scope in directive’s controller
  • ‘element’ object represents the element of the directive
    • Top level element
    • It’s jqLite object or jQuery object (if jQuery is included)

module

A module is a collection of services, directives, controllers, filters, and configuration information. angular.module is used to configure the $injector.

Create the module without dependency.

angular.module('module2', [])

Create with dependency.

angular.module('module3',
['module1', 'module2']);

Retrieve the module.

angular.module('module1')

ng-app = main module

<!DOCTYPE html>
<html ng-app='module3'>
</html>

Config and Run

angular.module('module1')
.config(function () {
// Inject only Providers and constants
});
angular.module('module1')
.run(function () {
// Inject only instances (like services) and constants.
});
  • module.config method fires before module.run method
  • All dependency modules get configured first
  • It doesn’t matter which modules are listed first as long as module declarations are listed before artifact declarations on that module

模块化

Splitting Javascript into Several Files.
每一个文件只包含一个artifact。在HTML中引入。

%23Angular%20JS%20-%20Modularize%0A@%28myblog%29%5Bangular%2C%20javascript%5D%0A%0AAngular%20JS%u6A21%u5757%u5316%uFF0C%u4E3B%u8981%u501F%u52A9%u4E8E3%u4E2A%u65B9%u5F0F%uFF1Adirective%2C%20component%2C%20module%0A%0A%23%23%20directive%20%26%203%20bindings%0A%23%23%23%202-way%20binding%20%22%3D%22%0A%60%60%60%0Afunction%20MyDirective%28%29%20%7B%0A%20%20var%20ddo%20%3D%20%7B%0A%09%20%20scope%3A%20%7B%0A%09%20%20%20%20myProp%3A%20%27%3DattributeName%27%0A%09%20%20%7D%0A%09%20%20...%0A%20%20%7D%3B%0A%20%20return%20ddo%3B%0A%7D%0A%60%60%60%0A%60%60%60%0A%3C%21--%20index.html%20--%3E%0A%3Cmy-directive%20attribute-name%3D%22outerProp%22%3E%0A%3C/my-directive%3E%0A%0A%3C%21--%20directive.html%20--%3E%0A%3Cp%3E%7B%7Bmy-prop%7D%7D%3C/p%3E%0A%60%60%60%0A%u6240%u6709%u5BF9attribute-name%u6216%u8005my-prop%u7684%u6539%u52A8%uFF0C%u4E24%u4E2Ahtml%u91CC%u90FD%u4F1A%u505A%u540C%u6B65%u4FEE%u6539%0A%23%23%23%201-way%20binding%20%22%26lt%3B%22%20and%20%22@%22%0A%60%60%60%0Afunction%20MyDirective%28%29%20%7B%0A%20%20var%20ddo%20%3D%20%7B%0A%09%20%20scope%3A%20%7B%0A%09%20%20%20%20prop%3A%20%27%26lt%3B%27%2C%0A%09%20%20%7D%2C%0A%09%20%20...%0A%20%20%7D%3B%0A%20%20return%20ddo%3B%0A%7D%0A%60%60%60%0A%u4E0E%u4E0A%u9762%u4E0D%u540C%uFF0C%u8FD9%u91CC%u53EA%u6709index.html%u624D%u80FD%u4FEE%u6539prop%u3002%0A%60%60%60%0Afunction%20MyDirective%28%29%20%7B%0A%20%20var%20ddo%20%3D%20%7B%0A%20%20scope%3A%20%7B%0A%20%20%20%20prop%3A%20%27@%27%2C%0A%20%20%7D%2C%0A%20%20...%0A%20%20%7D%3B%0A%20%20return%20ddo%3B%0A%7D%0A%60%60%60%0A%u8FD9%u91CCprop%u5C31%u53EA%u80FD%u7ED1%u5B9Aindex.html%u91CC%u7684%u5C5E%u6027%u53D8%u91CF%uFF0C%u5982%u4E0B%0A%60%60%60%0A%3Cmy-directive%20my-attribute%3D%22%7B%7BouterProp%7D%7D%22%3E%0A%3C/my-directive%3E%0A%60%60%60%0A%u53EF%u89C1my-attribute%u91CC%u9762%u4E0D%u80FD%u6709%u53D8%u91CF%uFF08%u4F8B%u5982controller%u6216%u8005controller%u7684%u6210%u5458%uFF09%u3002%u800C%u4E14%u548C%27%26lt%3B%27%u7C7B%u4F3C%uFF0C%27@%27%u4E5F%u662F%u5355%u5411%u7684%u3002%0A%0A%23%23%20%u7B80%u5316%u7248%u7684directive%20-%20component%0A%60%60%60%0Aangular.module%28%27App%27%2C%20%5B%5D%29%0A.component%28%27myComponent%27%2C%20%7B%0A%09templateUrl%3A%20%27template.html%27%2C%0A%09controller%3A%20CompController%2C%0A%09bindings%3A%20%7B%0A%09%09prop1%3A%20%27%26lt%3B%27%2C%0A%09%09prop2%3A%20%27@%27%2C%0A%09%09onAction%3A%20%27%26%27%0A%09%7D%0A%7D%29%3B%0A%60%60%60%0A%u8BED%u6CD5%u57FA%u672C%u4E0Edirective%u7C7B%u4F3C%uFF0C%u7528bindings%u53D6%u4EE3%u4E86scope%u3002%0A%3E%u2018bindings%u2019%20object%20is%20the%20isolate%20scope%20param%09mapping%20definition.%20%0A%0Aisolate%20scope%u91CC%u9762%u6CA1%u6709%u53CC%u5411%u7ED1%u5B9A%u4E86%uFF0C%u660E%u786E%u4E86%u8F93%u5165%u8F93%u51FA%uFF1A%0A-%20%27%26lt%3B%27%2C%20%27@%27%u4F5C%u4E3A%u8F93%u5165%0A-%20%27%26%27%u4F5C%u4E3A%u8F93%u51FA%uFF0C%u610F%u601D%u662F%u901A%u8FC7%u7528%27%26%27%u6CE8%u518Ccallback%u6765%u5C06%u8F93%u51FA%u9001%u5230%u76F8%u5E94%u7684%u6A21%u5757%0A%0A%23%23%23%20%u4F7F%u7528%27%26%27%u7684%u57FA%u672C%u65B9%u6CD5%0A%u8C03%u7528component%u7684DOM%u5199%u6CD5%u3002%0A%60%60%60%0A%3Cmy-component%0A%09prop1%3D%22val-1%22%0A%09prop2%3D%22@parentProp%22%0A%09on-action%3D%22parentFunction%28myArg%29%22%3E%0A%09%7B%7B%20%24ctrl.prop1.prop%20%7D%7D%20and%20%7B%7B%20%24ctrl.prop2%20%7D%7D%0A%3C/my-component%3E%0A%60%60%60%0Atemplate%u7684%u5199%u6CD5%uFF0Ccomponent%u7684%u5199%u6CD5%u5982%u4E0A%u4E00%u8282%u7684%u4F8B%u5B50%u3002%0A%60%60%60%0A%3Cdiv%0A%09ng-click%3D%22%24ctrl.onAction%28%7BmyArg%3A%20%27val%27%7D%29%22%3E%0A%09%7B%7B%20%24ctrl.prop1.prop%20%7D%7D%20and%20%7B%7B%20%24ctrl.prop2%20%7D%7D%0A%3C/div%3E%0A%60%60%60%0A%u6CE8%u610F%uFF1A%0A-%20onAction%u5728%u6807%u7B7E%u91CC%u7684%u5199%u6CD5%u662Fon-action%0A-%20myArg%u5728template%u4E2D%u662Fkey%20name%u3002value%u503C%u53EF%u4EE5%u6765%u81EA%u4E8Econtroller%28%24ctrl%29%uFF0C%u4EA6%u6216bindings%u4E2D%u7684%27%26lt%3B%27%2C%20%27@%27%uFF0C%u65B9%u6CD5%u5982%u4E0B%uFF0C%u901A%u8FC7inject%20controller%u3002%0A%0A%60%60%60%0Aangular.module%28%27MenuApp%27%29%0A.controller%28%27ItemListController%27%2C%20ItemListController%29%0A%0AItemListController.%24inject%20%3D%20%5B%27items%27%5D%0Afunction%20ItemListController%28items%2C%20currentItem%29%20%7B%0A%20%20var%20list%20%3D%20this%0A%20%20list.items%20%3D%20items%0A%7D%0A%60%60%60%0A%0A%23%23%23%20Components%20Lifecycle%0A-%20%24onInit%20%u2013%20controller%20initialization%20code%0A-%20%24onChanges%28changeObj%29%20%u2013%20called%20whenever%20one-way%20bindings%20are%20updated%0A%09-%20changeObj.currentValue%2C%20changeObj.previousValue%0A-%20%24postLink%20%u2013%20similar%20to%20%u2018link%u2019%20in%20directive%0A-%20%24onDestroy%20%u2013%20when%20scope%20is%20about%20to%20be%20destroyed%0A%0A%3E%20**Link%20for%20directive**%0A-%20DOM%20manipulation%20is%20usually%20done%20inside%20of%20the%20link%20function%0A%09-%20Declared%20on%20the%20DDO%0A-%20Link%20function%20does%20not%20support%20injection%0A%09-%20To%20use%20injected%20components%2C%20services%2C%20inject%20them%20into%20directive%0A-%20%u2018scope%u2019%20parameter%20is%20the%20exact%20%24scope%20in%20directive%u2019s%20controller%0A-%20%u2018element%u2019%20object%20represents%20the%20element%20of%20the%20directive%0A%09-%20Top%20level%20element%0A%09-%20It%u2019s%20jqLite%20object%20or%20jQuery%20object%20%28if%20jQuery%20is%20included%29%0A%0A%23%23%20module%0A%3EA%20module%20is%20a%20collection%20of%20services%2C%20directives%2C%20controllers%2C%20filters%2C%20and%20configuration%20information.%20angular.module%20is%20used%20to%20configure%20the%20%24injector.%0A%0ACreate%20the%20module%20without%20dependency.%0A%60%60%60%0Aangular.module%28%27module2%27%2C%20%5B%5D%29%0A%60%60%60%0ACreate%20with%20dependency.%0A%60%60%60%0Aangular.module%28%27module3%27%2C%0A%09%09%09%09%5B%27module1%27%2C%20%27module2%27%5D%29%3B%0A%60%60%60%0ARetrieve%20the%20module.%0A%60%60%60%0Aangular.module%28%27module1%27%29%0A%60%60%60%0Ang-app%20%3D%20main%20module%0A%60%60%60%0A%3C%21DOCTYPE%20html%3E%0A%3Chtml%20ng-app%3D%27module3%27%3E%0A%u2026%0A%3C/html%3E%0A%60%60%60%0A%0A%23%23%23%20Config%20and%20Run%0A%60%60%60%0Aangular.module%28%27module1%27%29%0A.config%28function%20%28%29%20%7B%0A%09//%20Inject%20only%20Providers%20and%20constants%09%0A%09%u2026%0A%7D%29%3B%0A%60%60%60%0A%0A%60%60%60%0Aangular.module%28%27module1%27%29%0A.run%28function%20%28%29%20%7B%0A%09//%20Inject%20only%20instances%20%28like%20services%29%20and%20constants.%0A%09%u2026%0A%7D%29%3B%0A%60%60%60%0A%0A-%20module.config%20method%20fires%20before%20module.run%20method%0A-%20All%20dependency%20modules%20get%20configured%20first%0A-%20It%20doesn%u2019t%20matter%20which%20modules%20are%20listed%20first%20as%20long%20as%20module%20declarations%20are%20listed%20before%20artifact%20declarations%20on%20that%20module%0A%0A%23%23%20%u6A21%u5757%u5316%0ASplitting%20Javascript%20into%20Several%20Files.%0A%u6BCF%u4E00%u4E2A%u6587%u4EF6%u53EA%u5305%u542B%u4E00%u4E2Aartifact%u3002%u5728HTML%u4E2D%u5F15%u5165%u3002%0A%21%5BAlt%20text%7C600x0%5D%28./1506329033574.png%29%0AHTML%u4E2D%u5F15%u5165script%uFF0C%u53EA%u662F%u4E00%u79CD%u9884%u7F16%u8BD1%uFF0C%u7C7B%u4F3CC%u8BED%u8A00%u7684include%uFF0C%u53EA%u4F1A%u628A%u5BF9%u5E94js%u6587%u4EF6%u7684%u4EE3%u7801%u76F4%u63A5%u63D2%u5165HTML%u6587%u4EF6%u3002%0A%0A

Edit

Overview

I bought an Amazon Echo during my last trip to Boise Idaho in US. It is cool product to respond quickly and works so well in acceptable noisy environment. She is a good buddy for my 5 year-old boy to play with and learn English from.
And I want her do more for more automation stuff at home. All my appliance are traditional ones. I bought them 2 years ago when I moved to the new condo. That’s not a long time passed but I don’t even know how technology changes so fast. Every electrical stuff becomes smart even a socket or a plug. That’s a amazing!
With Alexa, I think I can do more to make my home smarter even with the old style appliances.
The first thing comes into my eys is Broadlink universal remote control. It’s actually a converter for WiFi to IrDa and RF signal. Here is the product link. With the app provided by Broadlink, it can learn any IrDA or RF code of the remote controller and build in software virtual remote controller in the app to control the appliances those it can reach. There are 2 apps in Play Store:

But only using Broadlink remote controller is not perfect. You have to pick up your phone and click several times to open the app and corresponding virtual remote controller. This is not what I want.

To integrate Alexa, there are several solutions from time Googling.

  1. The appliance is born to be smart. And the vendor provides Alexa skills to control the appliance. For example, Philips HUE light, Belkin Wemo and etc.
  2. Integrate Broadlink into Alexa. There are also many solutions for this, including:
    1) Android + Broadlink RM Plugin + Alexa
    2) Domoticz/Home Assistant + HA-Bridge + Alexa
    3) Domoticz + python plugin + controlicz + Alexa
    4) Broadlink native skill + Alexa
  3. Domoticz/Home Assistant + HomeBridge + Siri

Domoticz and Home Assistant are both Home Automation System. There are a lot of people practicing them and post their experience on Internet. (They help me a lot. And this is the time I want to share my experience with others.) And both systems have their own advantages and disadvantages. The reference link 1 is the comparison among 5 popular automation systems.
I choose Domoticz is because it has native support the Synology NAS OS - DSM. Home NAS is in 7*24 hours service. I don’t need to setup extra hardware for the system. With this idea in mind, I practice solution 2)~4) in the item 2. I will consolidate all of them in this post below. The final solution architecture is shown as below:

Domoticz into Synology

This is quite simple. Please see the official website - Domoticz for Synology NAS. Download Domoticz for Synology DSM 6.1 with Python Plugin Beta. Python plugin is very important, don’t miss it. It will be used in after chapters.
The file you download is a .spk file. This is a installable package of Synology DSM. And you can install it by clicking “Manual Install” in “Package Center”. After success, it will shown as below:

Other Dependency

Domoticz’s plugin system depends on Python3
broadlink-http-rest server depends on Python2
HA-Bridge Server depends on Java8
You’d better to have also “git” installed. That will be much easier to clone GitHub projects.
The 4 packages in Synology package center look like below.

Broadlink into Domoticz

Domoticz Broadlink Python Plugin + Controlicz

This part takes the most issues and I spent several nights fixing the issues. I will have the issue list below.
Domoticz Broadlink plugin and broadlink-http-rest are all based on python-broadlink. So first of all, clone the project python-broadlink from GitHub.

Install python-broadlink

In many posts, people suggest to use pip to install the library of python-broadlink. But I found an issue of doing it this way. python-broadlink once was built based on pycrypto, which is out of support. And then the author switch to pyaes. But I think he didn’t resolve the dependency between python-broadlink and pycrypto. If you install python-broadlink on Synology using pip, it will run into issues of compiling pycrypto. Apparently, Synology DSM doesn’t have compilation environment (GCC and etc.).
My suggested steps are:

  • Make sure your python is linked to Python3. (ex. ln -s /usr/local/bin/python3.5 /usr/bin/python)
  • git clone python-broadlink && cd python-broadlink
  • python setup.py install
  • python -m pip install pyaes. Because pip3 cannot be installed into Synology DSM, even it’s installed by python3.5 get-pip.py. The script will also install pip2.

Install Domoticz Python Plugin

Reference link 3 is a Domoticz wiki page provided by the plugin author. The post is mainly worked out based on Windows system, but referring the others chapters, it’s still feasible. The installation steps are:

  • Download plugin files from dropbox.
  • The plugin folder is in/usr/local/domoticz/var/plugins/BroadlinkRM2/. Copy the files to this folderincluding:
    • plugin.py -main file
    • plugin_send.py
    • plugin_http.py
    • plugin_http.sh
      Be noted: the plugin folder is not /usr/local/domoticz/plugins/BroadlinkRM2/, although there is an example folder in that directory. But when I put plugin files in that directory, the plugin won’t be loaded correctly.
  • Stop and Run Domoticz in Package Center of Synology DSM. And do this every time you update the plugin python file.

So far, the installation finished. You should be able to see the Broalink hardware type in the pull-down list in Setup->Hardware.

Create Broadlink Hardware

Following the author’s steps, you should be able to create broadlink hardware in Domoticz web page and discover broadlink devices.
To create the hardware as below:

Import Devices from E-Control App导入易控(e-control)设备

Refer to reference link 3 for detailed description. It’s in User Guide->Inside Domoticz->xxx - import chapter. Author may not work with multi-byte language, such as Chinese. If there are Chinese characters in the imported json* files, the plugin will crash. See issue 3 for solution.
Another point should be noted, when WebStart is triggered, Domoticz will setup a tiny web service running on port 9000 to handle the files uploaded from mobile devices. Make sure you configure the Synology firewall correctly.

Controlicz

This seems to be a new service. I don’t see people mention it when I’m reading through Google results. I happened to see it in Alexa skill page. Using Controlicz, you don’t need to simulate your appliances as Philips HUI light using HA-Bridge. But it has its own pros and cons with Broadlink remote controller. There is a dedicated section comparing Controlicz and HA-Bridge.
To use Controlicz:

  • Register Controlicz using your Alexa’s credential (email and password)
  • Enable skill and say “discover device” to Alexa.
  • All done

Be noted: Controlicz asks for:

  • Domoticz service should be exposed to external access. That means you need to map port and DDNS. Because I’m using Synology, that’s not a problem to me.
  • Controlicz asks Domoticz running on HTTPS protocol. That means you have to enable the Domoticz HTTPS port and corresponding Firewall configurations.

Issue List

So far, if you are lucky enough to succeed in all above steps, you can say “Turn on the TV” to Alexa. The TV should respond to you quickly and correctly. If unfortunately it doesn’t, don’t worry. Check the issue list to see if it helps.

Import Error “broadlink”

You have installed python-broadlink already, and you can import it from Python REPL command line. Why is the error still reported? The direct cause is the PYTHONPATH is incorrect. But when I check the code in plugin.py, I see the process of PYTHONPATH as here:

if sys.platform.startswith('linux'):
# linux specific code here
# doesn't work even if set dist-packages => site-packages
sys.path.append(os.path.dirname(os.__file__) + '/dist-packages')
elif sys.platform.startswith('darwin'):
# mac
sys.path.append(os.path.dirname(os.__file__) + '/site-packages')
elif sys.platform.startswith('win32'):
# win specific
sys.path.append(os.path.dirname(os.__file__) + '\site-packages')

The path to broadlink library is /usr/local/lib/python3.5/site-packages/broadlink-0.5-py3.5.egg/broadlink/. Even when I correct the “dist-package” to “site-packages” in above code snippet, it still cannot work. However, the solution is quite simple. Just copy broadlink to the plugins folder.

cp -r /usr/local/lib/python3.5/site-packages/broadlink-0.5-py3.5.egg/broadlink/ /usr/local/domoticz/var/plugins/BroadlinkRM2/

Error Connecting to Broadlink device

There are 2 GitHub issue link for reference:

# broadlink/__init__.py
160 def encrypt_pyaes(self, payload):
161 aes = pyaes.AESModeOfOperationCBC(self.key, iv = bytes(self.iv))
162 return "".join([aes.encrypt(bytes(payload[i:i+16])) for i in range(0, len(payload), 16)]) # <==== Error is here

bytes in python3 returns class bytes, whereas in python2, it returns plain string. See here for experiments:

Python2
>>> type(bytes([1,2,3]))
<type 'str'>

Python3
>>> type(bytes([1,2,3]))
<class 'bytes'>
>>> "".join([bytes([1,2]),bytes([2,3])]) <==== You cannot cat 2 bytes class objects as string
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: sequence item 0: expected str instance, bytes found
>>> b''.join([bytes([1,2]),bytes([2,3])]) <==== This is the good way
b'\x01\x02\x02\x03'

The correct way to concatenating is to use binary string。

Chinese characters in Domoticz imported files

In Python3, if a file has Chinese character, you cannot read files and decode strings as below:

with open('jsonFile') as f:
textStr = f.read() <=== Python3 will complain about the codec error here.
textStr.decode('utf8')

The correct way is:

with open('jsonFile', encoding='utf8'):
textStr = f.read() <=== Everything here has already been unicode.

Update all lines of opening the imported json* files. And one more thing you need to pay attention to, which is for ConfigParser. You can update the encoding as below for them.

config = configparser.ConfigParser()
config.read(path, encoding='utf8')

Try it, it should do with no problems, aren’t you?

Domoticz + broadlink-http-rest + HA-Bridge

With HA-Bridge, you don’t need Domoticz Python plugin. You can create dummy hardware in Domoticz web page as below picture.

Install HA-Bridge

The whole idea of HA-Bridge is to simulate any device as Philips HUE light, which has native support in Alexa. And HA-Bridge can import all Domoticz devices easily. The cons is it can only support open/close and dim settings like you are operating a HUE light.
HA-Bridge is a jar package works with Java 8. And we need to run it in Synology background as a service. Refer to the post here -Run as a service on Synology for running it as a service.
The command line is nohup java -jar -Dserver.port=8085 /ha-bridge-4.5.6.jar &. Put it in Synology Scheduled Tasks, and then you don’t need to worry about the reboot.

Install broadlink-http-rest

Clone the project from GitHub - broadlink-http-rest. Just as HA-Bridge, it also needs to be in background as service. Otherwise, it will quite as soon as SSH connection is cut.The command line is nohup python broadlink-http-rest/server.py &. Be noted, make sure you are using “nohup”, otherwise it will still quite as SSH is disconnected even if you specify the “&”

Firewall & Scheduled Tasks

Configure the firewall:

  • 8085: HA-Bridge server
  • 8080: broadlink-http-rest server
  • 8443: Domoticz HTTPS service
  • 9000: Domoticz Broadlink web service

Also the scheduled tasks:

Broadlink into Domoticz

The steps are:

  • In Setup->Hardware, create a Dummy Hardware
  • In the same page and in the created item, click “Create Virtual Sensors”
  • In Switches tab, find the virtual sensor you just created, click Edit and configure “On Action” and “Off Action”.

Domoticz into HA-Bridge

Please refer to reference link 2. From the chapter of Configuring HA bridge, it starts to describe how to configure HA-Bridge to import devices from Domoticz web service.

HA-Bridge vs. Controlicz

General Smart Home

Controlicz can integrate all type of devices into Alexa. For example, if Alexa doesn’t have native support of this smart home device, you can build up a Domoticz server in your home and integrate it into Alexa by Controlicz. According to the description in Controlicz official site, it doesn’t only support on/off switches, but also other normal appliance like TV, AC and etc. And Domoticz apparently support a lot more devices than Alexa does.

Broadlink Integration

To integrate Broadlink, these 2 have their own pros and cons.
Broadlink is a universal remote controller. So every devices on Broadlink is a push button device. Each one only has one action, which is being triggered. This is even weaker than the on/off switch, which at lease has 2 actions - on or off.
For example, there is a button on Fan’s remote controller. Pushing it once, it will turn on the fan and twice will turn off it. Through Controlicz, you have to say “Turn on the Fan” to Alexa to turn on the fan and you have to say the same sentence to close it. Is it weird?
If you are using HA-Bridge, you can say “Turn on …” to turn on and “Turn off …” to turn off. Is it better?
The biggest benefit of using Controlicz to integrate Domoticz is it can import all devices pre-defined in E-Control app. It can fully make use of the E-Control’s GUI. You don’t need to add virtual sensors one by one manually.

Broadlink Native Skill + IHC

After bunch of work above, I happened to see there is a native support broadlink Alexa skill called “broadlink”. It should work with the latest version of IHC app. The bad news is both the skill and the app have very low review scores.

  • IHC playstore 2.2 stars
  • Alexa Broadlink Skill 2.3 stars

According to the official examples, it can only support TV. And as my own experiments, when I added a TV and an AC, Alexa can only discover the TV and no AC.

Reference Links

  1. 5 open source home automation tools
  2. Alex - Domoticz
  3. Domoticz Wiki Plugins/BroadlinkRM2
  4. #原创新人# 群晖安装broadlink-http-rest代替RMBridge_生活记录_什么值得买
%23%20Smart%20Home%20with%20Domoticz%20+%20BroadLink%20+%20Synology%20+%20Amazon%20Echo%0A@%28myblog%29%5Bsynology%2C%20broadlink%2C%20domoticz%2C%20IoT%5D%0A%0A%23%23Overview%0AI%20bought%20an%20Amazon%20Echo%20during%20my%20last%20trip%20to%20Boise%20Idaho%20in%20US.%20It%20is%20cool%20product%20to%20respond%20quickly%20and%20works%20so%20well%20in%20acceptable%20noisy%20environment.%20She%20is%20a%20good%20buddy%20for%20my%205%20year-old%20boy%20to%20play%20with%20and%20learn%20English%20from.%0AAnd%20I%20want%20her%20do%20more%20for%20more%20automation%20stuff%20at%20home.%20All%20my%20appliance%20are%20traditional%20ones.%20I%20bought%20them%202%20years%20ago%20when%20I%20moved%20to%20the%20new%20condo.%20That%27s%20not%20a%20long%20time%20passed%20but%20I%20don%27t%20even%20know%20how%20technology%20changes%20so%20fast.%20Every%20electrical%20stuff%20becomes%20smart%20even%20a%20socket%20or%20a%20plug.%20That%27s%20a%20amazing%21%0AWith%20Alexa%2C%20I%20think%20I%20can%20do%20more%20to%20make%20my%20home%20smarter%20even%20with%20the%20old%20style%20appliances.%0AThe%20first%20thing%20comes%20into%20my%20eys%20is%20Broadlink%20universal%20remote%20control.%20It%27s%20actually%20a%20converter%20for%20WiFi%20to%20IrDa%20and%20RF%20signal.%20Here%20is%20the%20product%20%5Blink%5D%28https%3A//detail.tmall.com/item.htm%3Fspm%3Da230r.1.14.8.ebb2eb2Gs49Vt%26id%3D43623542607%26cm_id%3D140105335569ed55e27b%26abbucket%3D13%26skuId%3D77095242268%29.%20With%20the%20app%20provided%20by%20Broadlink%2C%20it%20can%20learn%20any%20IrDA%20or%20RF%20code%20of%20the%20remote%20controller%20and%20build%20in%20software%20virtual%20remote%20controller%20in%20the%20app%20to%20control%20the%20appliances%20those%20it%20can%20reach.%20There%20are%202%20apps%20in%20Play%20Store%3A%0A-%20%5Be-control%5D%28https%3A//play.google.com/store/apps/details%3Fid%3Dcom.broadlink.rmt%29%20%0A-%20%5BIHC%5D%28https%3A//play.google.com/store/apps/details%3Fid%3Dcn.com.broadlink.econtrol.plus%29%0A%0ABut%20only%20using%20Broadlink%20remote%20controller%20is%20not%20perfect.%20You%20have%20to%20pick%20up%20your%20phone%20and%20click%20several%20times%20to%20open%20the%20app%20and%20corresponding%20virtual%20remote%20controller.%20This%20is%20not%20what%20I%20want.%0A%0ATo%20integrate%20Alexa%2C%20there%20are%20several%20solutions%20from%20time%20Googling.%0A1.%20The%20appliance%20is%20born%20to%20be%20smart.%20And%20the%20vendor%20provides%20Alexa%20skills%20to%20control%20the%20appliance.%20For%20example%2C%20Philips%20HUE%20light%2C%20Belkin%20Wemo%20and%20etc.%0A2.%20Integrate%20Broadlink%20into%20Alexa.%20There%20are%20also%20many%20solutions%20for%20this%2C%20including%3A%0A%091%29%20Android%20+%20%5BBroadlink%20RM%20Plugin%5D%28https%3A//play.google.com/store/apps/details%3Fid%3Dus.originally.tasker%29%20+%20Alexa%0A%092%29%20Domoticz/Home%20Assistant%20+%20%20%5BHA-Bridge%5D%28https%3A//github.com/bwssytems/ha-bridge%29%20+%20Alexa%0A%093%29%20Domoticz%20+%20python%20plugin%20+%20controlicz%20+%20Alexa%0A%094%29%20Broadlink%20native%20skill%20+%20Alexa%20%0A3.%20Domoticz/Home%20Assistant%20+%20HomeBridge%20+%20Siri%0A%0ADomoticz%20and%20Home%20Assistant%20are%20both%20Home%20Automation%20System.%20There%20are%20a%20lot%20of%20people%20practicing%20them%20and%20post%20their%20experience%20on%20Internet.%20%28They%20help%20me%20a%20lot.%20And%20this%20is%20the%20time%20I%20want%20to%20share%20my%20experience%20with%20others.%29%20And%20both%20systems%20have%20their%20own%20advantages%20and%20disadvantages.%20The%20reference%20link%201%20is%20the%20comparison%20among%205%20popular%20automation%20systems.%0AI%20choose%20Domoticz%20is%20because%20it%20has%20native%20support%20the%20Synology%20NAS%20OS%20-%20DSM.%20Home%20NAS%20is%20in%207*24%20hours%20service.%20I%20don%27t%20need%20to%20setup%20extra%20hardware%20for%20the%20system.%20With%20this%20idea%20in%20mind%2C%20I%20practice%20solution%202%29%7E4%29%20in%20the%20item%202.%20I%20will%20consolidate%20all%20of%20them%20in%20this%20post%20below.%20The%20final%20solution%20architecture%20is%20shown%20as%20below%3A%0A%0A%21%5BAlt%20text%5D%28./1501339487875.png%29%0A%0A%23%23%20Domoticz%20into%20Synology%0AThis%20is%20quite%20simple.%20Please%20see%20the%20official%20website%20-%20%5BDomoticz%20for%20Synology%20NAS%5D%28http%3A//www.jadahl.com/%29.%20Download%20%5BDomoticz%20for%20Synology%20DSM%206.1%20with%20Python%20Plugin%20Beta%5D%28http%3A//www.jadahl.com/domoticz_beta_6.1_python/%29.%20Python%20plugin%20is%20very%20important%2C%20don%27t%20miss%20it.%20It%20will%20be%20used%20in%20after%20chapters.%0AThe%20file%20you%20download%20is%20a%20.spk%20file.%20This%20is%20a%20installable%20package%20of%20Synology%20DSM.%20And%20you%20can%20install%20it%20by%20clicking%20%22Manual%20Install%22%20in%20%22Package%20Center%22.%20After%20success%2C%20it%20will%20shown%20as%20below%3A%0A%0A%21%5BAlt%20text%5D%28./1501340457645.png%29%0A%0A%23%23%20Other%20Dependency%0ADomoticz%27s%20plugin%20system%20depends%20on%20Python3%0Abroadlink-http-rest%20server%20depends%20on%20Python2%0A%5BHA-Bridge%20Server%5D%28https%3A//github.com/bwssytems/ha-bridge%29%20depends%20on%20Java8%0AYou%27d%20better%20to%20have%20also%20%22git%22%20installed.%20That%20will%20be%20much%20easier%20to%20clone%20GitHub%20projects.%0AThe%204%20packages%20in%20Synology%20package%20center%20look%20like%20below.%0A%21%5BAlt%20text%5D%28./1501340796886.png%29%21%5BAlt%20text%5D%28./1501340823935.png%29%21%5BAlt%20text%5D%28./1501340847466.png%29%0A%0A%23%23%20Broadlink%20into%20Domoticz%0A%0A%23%23%23%20Domoticz%20Broadlink%20Python%20Plugin%20+%20Controlicz%0AThis%20part%20takes%20the%20most%20issues%20and%20I%20spent%20several%20nights%20fixing%20the%20issues.%20I%20will%20have%20the%20issue%20list%20below.%0ADomoticz%20Broadlink%20plugin%20and%20%5Bbroadlink-http-rest%5D%28https%3A//github.com/radinsky/broadlink-http-rest%29%20are%20all%20based%20on%20%5Bpython-broadlink%5D%28https%3A//github.com/mjg59/python-broadlink%29.%20So%20first%20of%20all%2C%20clone%20the%20project%20python-broadlink%20from%20GitHub.%0A%0A%23%23%23%23%20Install%20python-broadlink%0AIn%20many%20posts%2C%20people%20suggest%20to%20use%20pip%20to%20install%20the%20library%20of%20python-broadlink.%20But%20I%20found%20an%20issue%20of%20doing%20it%20this%20way.%20python-broadlink%20once%20was%20built%20based%20on%20pycrypto%2C%20which%20is%20out%20of%20support.%20And%20then%20the%20author%20switch%20to%20pyaes.%20But%20I%20think%20he%20didn%27t%20resolve%20the%20dependency%20between%20python-broadlink%20and%20pycrypto.%20If%20you%20install%20python-broadlink%20on%20Synology%20using%20pip%2C%20it%20will%20run%20into%20issues%20of%20compiling%20pycrypto.%20Apparently%2C%20Synology%20DSM%20doesn%27t%20have%20compilation%20environment%20%28GCC%20and%20etc.%29.%0AMy%20suggested%20steps%20are%3A%0A-%20Make%20sure%20your%20python%20is%20linked%20to%20Python3.%20%28ex.%20ln%20-s%20/usr/local/bin/python3.5%20/usr/bin/python%29%0A-%20git%20clone%20python-broadlink%20%26%26%20cd%20python-broadlink%0A-%20%60python%20setup.py%20install%60%0A-%20%60python%20-m%20pip%20install%20pyaes%60.%20Because%20pip3%20cannot%20be%20installed%20into%20Synology%20DSM%2C%20even%20it%27s%20installed%20by%20%60python3.5%20get-pip.py%60.%20The%20script%20will%20also%20install%20pip2.%0A%0A%23%23%23%23%20Install%20Domoticz%20Python%20Plugin%0AReference%20link%203%20is%20a%20Domoticz%20wiki%20page%20provided%20by%20the%20plugin%20author.%20The%20post%20is%20mainly%20worked%20out%20based%20on%20Windows%20system%2C%20but%20referring%20the%20others%20chapters%2C%20it%27s%20still%20feasible.%20The%20installation%20steps%20are%3A%0A-%20Download%20%5Bplugin%20files%5D%28https%3A//www.dropbox.com/sh/htyghey9e402u4y/AACeb1cXqaPd9gBVl5TL3H36a%3Fdl%3D0%29%20from%20dropbox.%0A-%20The%20plugin%20folder%20is%20in%60/usr/local/domoticz/var/plugins/BroadlinkRM2/%60.%20Copy%20the%20files%20to%20this%20folderincluding%3A%0A%09-%20plugin.py%20-main%20file%0A%09-%20plugin_send.py%0A%09-%20plugin_http.py%0A%09-%20plugin_http.sh%0A%09Be%20noted%3A%20the%20plugin%20folder%20is%20not%20%60/usr/local/domoticz/plugins/BroadlinkRM2/%60%2C%20although%20there%20is%20an%20example%20folder%20in%20that%20directory.%20But%20when%20I%20put%20plugin%20files%20in%20that%20directory%2C%20the%20plugin%20won%27t%20be%20loaded%20correctly.%0A-%20Stop%20and%20Run%20Domoticz%20in%20Package%20Center%20of%20Synology%20DSM.%20And%20do%20this%20every%20time%20you%20update%20the%20plugin%20python%20file.%0A%0ASo%20far%2C%20the%20installation%20finished.%20You%20should%20be%20able%20to%20see%20the%20Broalink%20hardware%20type%20in%20the%20pull-down%20list%20in%20Setup-%3EHardware.%0A%21%5BAlt%20text%5D%28./1500861858610.png%29%0A%0A%23%23%23%23%20Create%20Broadlink%20Hardware%0AFollowing%20the%20author%27s%20steps%2C%20you%20should%20be%20able%20to%20create%20broadlink%20hardware%20in%20Domoticz%20web%20page%20and%20discover%20broadlink%20devices.%0ATo%20create%20the%20hardware%20as%20below%3A%0A%21%5BAlt%20text%5D%28./1501368649742.png%29%0AIP%20address%20can%20be%20127.0.0.1%20and%20MAC%20address%20can%20be%20all%20zero.%20The%20plugin%20can%20trigger%20a%20discovery%20process%20to%20get%20the%20real%20IP%20address%20and%20MAC%20address.%20Refer%20to%20reference%20link%203%20for%20detailed%20description.%20But%20it%20won%27t%20be%20such%20smooth%20as%20you%20imagine.%20Refer%20to%20the%20issue%20list%20below.%0A%0A%23%23%23%23%20Import%20Devices%20from%20E-Control%20App%u5BFC%u5165%u6613%u63A7%28e-control%29%u8BBE%u5907%0ARefer%20to%20reference%20link%203%20for%20detailed%20description.%20It%27s%20in%20User%20Guide-%3EInside%20Domoticz-%3Exxx%20-%20import%20chapter.%20Author%20may%20not%20work%20with%20multi-byte%20language%2C%20such%20as%20Chinese.%20If%20there%20are%20Chinese%20characters%20in%20the%20imported%20json*%20files%2C%20the%20plugin%20will%20crash.%20See%20issue%203%20for%20solution.%0AAnother%20point%20should%20be%20noted%2C%20when%20WebStart%20is%20triggered%2C%20Domoticz%20will%20setup%20a%20tiny%20web%20service%20running%20on%20port%209000%20to%20handle%20the%20files%20uploaded%20from%20mobile%20devices.%20Make%20sure%20you%20configure%20the%20Synology%20firewall%20correctly.%0A%0A%23%23%23%20Controlicz%0AThis%20seems%20to%20be%20a%20new%20service.%20I%20don%27t%20see%20people%20mention%20it%20when%20I%27m%20reading%20through%20Google%20results.%20I%20happened%20to%20see%20it%20in%20Alexa%20skill%20page.%20Using%20Controlicz%2C%20you%20don%27t%20need%20to%20simulate%20your%20appliances%20as%20Philips%20HUI%20light%20using%20HA-Bridge.%20But%20it%20has%20its%20own%20pros%20and%20cons%20with%20Broadlink%20remote%20controller.%20There%20is%20a%20dedicated%20section%20comparing%20Controlicz%20and%20HA-Bridge.%0ATo%20use%20Controlicz%3A%0A-%20Register%20%5BControlicz%5D%28https%3A//www.controlicz.com/%29%20using%20your%20Alexa%27s%20credential%20%28email%20and%20password%29%0A-%20Enable%20skill%20and%20say%20%22discover%20device%22%20to%20Alexa.%0A-%20All%20done%0A%0A%3EBe%20noted%3A%20Controlicz%20asks%20for%3A%0A%3E-%20Domoticz%20service%20should%20be%20exposed%20to%20external%20access.%20That%20means%20you%20need%20to%20map%20port%20and%20DDNS.%20Because%20I%27m%20using%20Synology%2C%20that%27s%20not%20a%20problem%20to%20me.%0A%3E-%20Controlicz%20asks%20Domoticz%20running%20on%20HTTPS%20protocol.%20That%20means%20you%20have%20to%20enable%20the%20Domoticz%20HTTPS%20port%20and%20corresponding%20Firewall%20configurations.%0A%0A%23%23%23%20Issue%20List%0ASo%20far%2C%20if%20you%20are%20lucky%20enough%20to%20succeed%20in%20all%20above%20steps%2C%20you%20can%20say%20%22Turn%20on%20the%20TV%22%20to%20Alexa.%20The%20TV%20should%20respond%20to%20you%20quickly%20and%20correctly.%20If%20unfortunately%20it%20doesn%27t%2C%20don%27t%20worry.%20Check%20the%20issue%20list%20to%20see%20if%20it%20helps.%0A%0A%23%23%23%23%20Import%20Error%20%22broadlink%22%0AYou%20have%20installed%20python-broadlink%20already%2C%20and%20you%20can%20import%20it%20from%20Python%20REPL%20command%20line.%20Why%20is%20the%20error%20still%20reported%3F%20The%20direct%20cause%20is%20the%20PYTHONPATH%20is%20incorrect.%20But%20when%20I%20check%20the%20code%20in%20plugin.py%2C%20I%20see%20the%20process%20of%20PYTHONPATH%20as%20here%3A%0A%60%60%60%0Aif%20sys.platform.startswith%28%27linux%27%29%3A%0A%20%20%20%20%23%20linux%20specific%20code%20here%0A%20%20%20%20%23%20doesn%27t%20work%20even%20if%20set%20dist-packages%20%3D%3E%20site-packages%0A%20%20%20%20sys.path.append%28os.path.dirname%28os.__file__%29%20+%20%27/dist-packages%27%29%0Aelif%20sys.platform.startswith%28%27darwin%27%29%3A%0A%20%20%20%20%23%20mac%0A%20%20%20%20sys.path.append%28os.path.dirname%28os.__file__%29%20+%20%27/site-packages%27%29%0Aelif%20sys.platform.startswith%28%27win32%27%29%3A%0A%20%20%20%20%23%20%20win%20specific%0A%20%20%20%20sys.path.append%28os.path.dirname%28os.__file__%29%20+%20%27%5Csite-packages%27%29%0A%60%60%60%0AThe%20path%20to%20broadlink%20library%20is%20%60/usr/local/lib/python3.5/site-packages/broadlink-0.5-py3.5.egg/broadlink/%60.%20Even%20when%20I%20correct%20the%20%22dist-package%22%20to%20%22site-packages%22%20in%20above%20code%20snippet%2C%20it%20still%20cannot%20work.%20However%2C%20the%20solution%20is%20quite%20simple.%20Just%20copy%20broadlink%20to%20the%20plugins%20folder.%0A%3Ecp%20-r%20/usr/local/lib/python3.5/site-packages/broadlink-0.5-py3.5.egg/broadlink/%20/usr/local/domoticz/var/plugins/BroadlinkRM2/%0A%0A%23%23%23%23%20Error%20Connecting%20to%20Broadlink%20device%0AThere%20are%202%20GitHub%20issue%20link%20for%20reference%3A%0A-%20%5BError%20Connecting%20to%20Broadlink%20device%20%23107%5D%28https%3A//github.com/mjg59/python-broadlink/issues/107%29%0A-%20%5Blpad%20fix%20broken%3F%20%2397%5D%28https%3A//github.com/mjg59/python-broadlink/issues/97%29%0AIt%20may%20help%20you%2C%20but%20it%20is%20not%20the%20answer%20to%20my%20issue.%0AThe%20root%20cause%20is%20bytes%20function%20is%20different%20in%20Python2%20and%20Python3.%20The%20error%20happens%20here%3A%0A%60%60%60%0A%23%20broadlink/__init__.py%0A160%20%20%20def%20encrypt_pyaes%28self%2C%20payload%29%3A%0A161%20%20%20%20%20aes%20%3D%20pyaes.AESModeOfOperationCBC%28self.key%2C%20iv%20%3D%20bytes%28self.iv%29%29%0A162%20%20%20%20%20return%20%22%22.join%28%5Baes.encrypt%28bytes%28payload%5Bi%3Ai+16%5D%29%29%20for%20i%20in%20range%280%2C%20len%28payload%29%2C%2016%29%5D%29%20%23%20%3C%3D%3D%3D%3D%20Error%20is%20here%0A%60%60%60%0Abytes%20in%20python3%20returns%20class%20bytes%2C%20whereas%20in%20python2%2C%20it%20returns%20plain%20string.%20See%20here%20for%20experiments%3A%20%0A%60%60%60%0APython2%0A%3E%3E%3E%20type%28bytes%28%5B1%2C2%2C3%5D%29%29%0A%3Ctype%20%27str%27%3E%0A%0APython3%0A%3E%3E%3E%20type%28bytes%28%5B1%2C2%2C3%5D%29%29%0A%3Cclass%20%27bytes%27%3E%0A%3E%3E%3E%20%22%22.join%28%5Bbytes%28%5B1%2C2%5D%29%2Cbytes%28%5B2%2C3%5D%29%5D%29%20%20%3C%3D%3D%3D%3D%20You%20cannot%20cat%202%20bytes%20class%20objects%20as%20string%0ATraceback%20%28most%20recent%20call%20last%29%3A%0A%20%20File%20%22%3Cstdin%3E%22%2C%20line%201%2C%20in%20%3Cmodule%3E%0ATypeError%3A%20sequence%20item%200%3A%20expected%20str%20instance%2C%20bytes%20found%0A%3E%3E%3E%20b%27%27.join%28%5Bbytes%28%5B1%2C2%5D%29%2Cbytes%28%5B2%2C3%5D%29%5D%29%20%3C%3D%3D%3D%3D%20This%20is%20the%20good%20way%0Ab%27%5Cx01%5Cx02%5Cx02%5Cx03%27%0A%60%60%60%0AThe%20correct%20way%20to%20concatenating%20is%20to%20use%20binary%20string%u3002%0A%0A%23%23%23%23%20Chinese%20characters%20in%20Domoticz%20imported%20files%0AIn%20Python3%2C%20if%20a%20file%20has%20Chinese%20character%2C%20you%20cannot%20read%20files%20and%20decode%20strings%20as%20below%3A%0A%60%60%60%0Awith%20open%28%27jsonFile%27%29%20as%20f%3A%0A%09textStr%20%3D%20f.read%28%29%20%3C%3D%3D%3D%20Python3%20will%20complain%20about%20the%20codec%20error%20here.%0A%09textStr.decode%28%27utf8%27%29%0A%60%60%60%0AThe%20correct%20way%20is%3A%0A%60%60%60%0Awith%20open%28%27jsonFile%27%2C%20encoding%3D%27utf8%27%29%3A%0A%09textStr%20%3D%20f.read%28%29%20%3C%3D%3D%3D%20Everything%20here%20has%20already%20been%20unicode.%0A%60%60%60%0AUpdate%20all%20lines%20of%20opening%20the%20imported%20json*%20files.%20And%20one%20more%20thing%20you%20need%20to%20pay%20attention%20to%2C%20which%20is%20for%20ConfigParser.%20You%20can%20update%20the%20encoding%20as%20below%20for%20them.%0A%60%60%60%0Aconfig%20%3D%20configparser.ConfigParser%28%29%0Aconfig.read%28path%2C%20encoding%3D%27utf8%27%29%0A%60%60%60%0ATry%20it%2C%20it%20should%20do%20with%20no%20problems%2C%20aren%27t%20you%3F%0A%0A%23%23%23%20Domoticz%20+%20broadlink-http-rest%20+%20HA-Bridge%0AWith%20HA-Bridge%2C%20you%20don%27t%20need%20Domoticz%20Python%20plugin.%20You%20can%20create%20dummy%20hardware%20in%20Domoticz%20web%20page%20as%20below%20picture.%0A%21%5BAlt%20text%5D%28./1501376022176.png%29%0A%0A%23%23%23%23%20Install%20HA-Bridge%0AThe%20whole%20idea%20of%20HA-Bridge%20is%20to%20simulate%20any%20device%20as%20Philips%20HUE%20light%2C%20which%20has%20native%20support%20in%20Alexa.%20And%20HA-Bridge%20can%20import%20all%20Domoticz%20devices%20easily.%20The%20cons%20is%20it%20can%20only%20support%20open/close%20and%20dim%20settings%20like%20you%20are%20operating%20a%20HUE%20light.%0AHA-Bridge%20is%20a%20jar%20package%20works%20with%20Java%208.%20And%20we%20need%20to%20run%20it%20in%20Synology%20background%20as%20a%20service.%20Refer%20to%20the%20post%20here%20-%5BRun%20as%20a%20service%20on%20Synology%5D%28https%3A//github.com/bwssytems/ha-bridge/issues/310%29%20for%20running%20it%20as%20a%20service.%0AThe%20command%20line%20is%20%60nohup%20java%20-jar%20-Dserver.port%3D8085%20/ha-bridge-4.5.6.jar%20%26%60.%20Put%20it%20in%20Synology%20Scheduled%20Tasks%2C%20and%20then%20you%20don%27t%20need%20to%20worry%20about%20the%20reboot.%0A%0A%23%23%23%23%20Install%20broadlink-http-rest%0AClone%20the%20project%20from%20GitHub%20-%20%5Bbroadlink-http-rest%5D%28https%3A//github.com/radinsky/broadlink-http-rest%29.%20Just%20as%20HA-Bridge%2C%20it%20also%20needs%20to%20be%20in%20background%20as%20service.%20Otherwise%2C%20it%20will%20quite%20as%20soon%20as%20SSH%20connection%20is%20cut.The%20command%20line%20is%20%60nohup%20python%20broadlink-http-rest/server.py%20%26%60.%20Be%20noted%2C%20make%20sure%20you%20are%20using%20%22nohup%22%2C%20otherwise%20it%20will%20still%20quite%20as%20SSH%20is%20disconnected%20even%20if%20you%20specify%20the%20%22%26%22%0A%0A%23%23%23%23%20Firewall%20%26%20Scheduled%20Tasks%0AConfigure%20the%20firewall%3A%0A%21%5BAlt%20text%5D%28./1501376973009.png%29%0A-%208085%3A%20HA-Bridge%20server%0A-%208080%3A%20broadlink-http-rest%20server%0A-%208443%3A%20Domoticz%20HTTPS%20service%0A-%209000%3A%20Domoticz%20Broadlink%20web%20service%0A%0AAlso%20the%20scheduled%20tasks%3A%0A%21%5BAlt%20text%5D%28./1501377068095.png%29%0A%0A%23%23%23%23%20Broadlink%20into%20Domoticz%0AThe%20steps%20are%3A%0A-%20In%20Setup-%3EHardware%2C%20create%20a%20Dummy%20Hardware%0A-%20In%20the%20same%20page%20and%20in%20the%20created%20item%2C%20click%20%22Create%20Virtual%20Sensors%22%0A-%20In%20Switches%20tab%2C%20find%20the%20virtual%20sensor%20you%20just%20created%2C%20%20click%20Edit%20and%20configure%20%22On%20Action%22%20and%20%22Off%20Action%22.%0A%21%5BAlt%20text%5D%28./1501376159204.png%29%0AHere%20the%20on/off%20action%20is%20to%20use%20broadlink-http-rest%20service%20to%20send%20broadlink%20commands%20for%20controlling%20the%20appliances.%20Before%20sending%20the%20commands%2C%20you%20have%20to%20also%20use%20broadlink-http-rest%20service%20to%20let%20broadlink%20to%20learn%20the%20code.%20For%20example%2C%20type%20http%3A//192.168.1.2%3A8080/learnCommand/open-ac%20in%20your%20browser%2C%20broadlink%20RM%20will%20enter%20learning%20mode.%20The%20orange%20light%20will%20be%20turned%20on.%20Then%20you%20need%20to%20push%20the%20button%20on%20the%20AC%20remote%20controller%20to%20let%20broadlink%20RM%20learn%20the%20IrDA%20or%20RF%20codes.%20You%20can%20use%20any%20names%20for%20this%20command%20instead%20of%20%22open-ac%22.%20As%20soon%20as%20the%20broadlink%20learns%20the%20code%2C%20you%20can%20put%20the%20address%20of%20%20http%3A//192.168.1.2%3A8080/sendCommand/open-ac%20to%20test%20your%20command.%20If%20it%20works%2C%20you%20can%20put%20it%20into%20your%20virtual%20sensor%20configuration%20page.%0A%23%23%23%23%20Domoticz%20into%20HA-Bridge%0APlease%20refer%20to%20reference%20link%202.%20From%20the%20chapter%20of%20Configuring%20HA%20bridge%2C%20it%20starts%20to%20describe%20how%20to%20configure%20HA-Bridge%20to%20import%20devices%20from%20Domoticz%20web%20service.%20%0A%21%5BAlt%20text%5D%28./1501401044317.png%29%0AAfter%20adding%20device%20from%20any%20device%20source%20like%20Domoticz.%0A%21%5BAlt%20text%5D%28./1501403754697.png%29%0ANow%20you%20can%20discover%20the%20devices%20by%20Alexa%20and%20also%20turn%20on/off%20them.%0A%0A%23%23%23%23%20HA-Bridge%20vs.%20Controlicz%0A%23%23%23%23%23%20General%20Smart%20Home%0AControlicz%20can%20integrate%20all%20type%20of%20devices%20into%20Alexa.%20For%20example%2C%20if%20Alexa%20doesn%27t%20have%20native%20support%20of%20this%20smart%20home%20device%2C%20you%20can%20build%20up%20a%20Domoticz%20server%20in%20your%20home%20and%20integrate%20it%20into%20Alexa%20by%20Controlicz.%20According%20to%20the%20description%20in%20Controlicz%20official%20site%2C%20it%20doesn%27t%20only%20support%20on/off%20switches%2C%20but%20also%20other%20normal%20appliance%20like%20TV%2C%20AC%20and%20etc.%20And%20Domoticz%20apparently%20support%20a%20lot%20more%20devices%20than%20Alexa%20does.%0A%21%5BAlt%20text%5D%28./1501404140819.png%29%0AHA-Bridge%20is%20simulating%20HUE%20light.%20To%20Alexa%2C%20these%20HA-Bridge%20based%20device%20is%20a%20HUE%20light.%20So%20it%20can%20only%20support%20on/off%20and%20dim%203%20actions.%0AFrom%20this%20point%20of%20view%2C%20these%202%20should%20not%20be%20put%20together%20for%20comparison.%20Controlicz%20is%20much%20stronger%20than%20HA-Bridge.%0A%0A%23%23%23%23%23%20%20Broadlink%20Integration%0ATo%20integrate%20Broadlink%2C%20these%202%20have%20their%20own%20pros%20and%20cons.%0ABroadlink%20is%20a%20universal%20remote%20controller.%20So%20every%20devices%20on%20Broadlink%20is%20a%20push%20button%20device.%20Each%20one%20only%20has%20one%20action%2C%20which%20is%20being%20triggered.%20This%20is%20even%20weaker%20than%20the%20on/off%20switch%2C%20which%20at%20lease%20has%202%20actions%20-%20on%20or%20off.%0AFor%20example%2C%20there%20is%20a%20button%20on%20Fan%27s%20remote%20controller.%20Pushing%20it%20once%2C%20it%20will%20turn%20on%20the%20fan%20and%20twice%20will%20turn%20off%20it.%20Through%20Controlicz%2C%20you%20have%20to%20say%20%22Turn%20on%20the%20Fan%22%20to%20Alexa%20to%20turn%20on%20the%20fan%20and%20you%20have%20to%20say%20the%20same%20sentence%20to%20close%20it.%20Is%20it%20weird%3F%0AIf%20you%20are%20using%20HA-Bridge%2C%20you%20can%20say%20%22Turn%20on%20...%22%20to%20turn%20on%20and%20%22Turn%20off%20...%22%20to%20turn%20off.%20Is%20it%20better%3F%0AThe%20biggest%20benefit%20of%20using%20Controlicz%20to%20integrate%20Domoticz%20is%20it%20can%20import%20all%20devices%20pre-defined%20in%20E-Control%20app.%20It%20can%20fully%20make%20use%20of%20the%20E-Control%27s%20GUI.%20You%20don%27t%20need%20to%20add%20virtual%20sensors%20one%20by%20one%20manually.%0A%0A%23%23%23%20Broadlink%20Native%20Skill%20+%20IHC%0AAfter%20bunch%20of%20work%20above%2C%20I%20happened%20to%20see%20there%20is%20a%20native%20support%20broadlink%20Alexa%20skill%20called%20%22broadlink%22.%20It%20should%20work%20with%20the%20latest%20version%20of%20IHC%20app.%20The%20bad%20news%20is%20both%20the%20skill%20and%20the%20app%20have%20very%20low%20review%20scores.%0A-%20IHC%20playstore%202.2%20stars%0A-%20Alexa%20Broadlink%20Skill%202.3%20stars%0A%0AAccording%20to%20the%20official%20examples%2C%20it%20can%20only%20support%20TV.%20And%20as%20my%20own%20experiments%2C%20when%20I%20added%20a%20TV%20and%20an%20AC%2C%20Alexa%20can%20only%20discover%20the%20TV%20and%20no%20AC.%0A%0A%0A%23%23%20Reference%20Links%0A1.%20%5B5%20open%20source%20home%20automation%20tools%5D%28https%3A//opensource.com/life/16/3/5-open-source-home-automation-tools%29%0A2.%20%5BAlex%20-%20Domoticz%5D%28https%3A//www.domoticz.com/wiki/Alexa%29%0A3.%20%5BDomoticz%20Wiki%20Plugins/BroadlinkRM2%5D%28http%3A//www.domoticz.com/wiki/Plugins/BroadlinkRM2.html%23First_Time_Users%29%0A4.%20%5B%23%u539F%u521B%u65B0%u4EBA%23%20%u7FA4%u6656%u5B89%u88C5broadlink-http-rest%u4EE3%u66FFRMBridge_%u751F%u6D3B%u8BB0%u5F55_%u4EC0%u4E48%u503C%u5F97%u4E70%5D%28https%3A//post.smzdm.com/p/551024/%29

Edit

Overview

此事缘起于在美帝买了Amazon Echo,想着回来可以在家里多说英语,给儿子一些熏陶,也是个大玩具,说不定人家会爱上呢。后来想着整天对Alexa吼这吼那的,如果能唤他帮咱开空调,开电视,开灯,那就不用整天操心遥控器了吗。尤其,整天盯着遥控器的主,拎着遥控器一摇一摆就走了,然后撒着欢儿跑回来,遥控器就不知道丢哪儿了。
首先查到的是BroadLink配件,这其实是一个WiFi转红外+射频的配件。我买的是这个。通过其自带的学习模式,可以学习任何一个遥控器的遥控码。配合Broadlink自家的app就已经可以实现遥控器电子化了。Broadlink有安卓上有两个app:

不够完美的是仍然需要掏出个手机,翻找app,一堆乱点。这不是我想要的。

继续百度,Google,如果要整合Amazon Echo——下文简称Alexa以图方便,有以下几种方案:

  1. 本身是智能化家居,并且厂家有对应的Skill,enable该Skill即可,好处是各种原生的设置,例如:空调设几度,灯开什么颜色
  2. 集成BroadLink,让Alexa操作BroadLink,此方案仍然有多种子方案:
    1) Android + Broadlink RM Plugin + Alexa
    2) Domoticz/Home Assistant + HA-Bridge + Alexa
    3) Domoticz + python plugin + controlicz + Alexa
    4) Broadlink原生skill + Alexa
  3. Domoticz/Home Assistant + HomeBridge + Siri

其中Domoticz和Home Assistant均为Home Automation System,网上两者都有很多人实践,各有优劣。在参考链接中专门对5种不同的系统进行了比较。因为我有Synology NAS,又可以原生集成,所以毫不犹豫选择了Domoticz。充分发挥NAS 7*24小时不下线的优势,实现智能家庭网关。针对第二点中的后3个(2-4)方案全部进行了实现。后面会一一列述。最终方案架构如下图:

Domoticz into Synology

这个超级简单。有官方支持Domoticz for Synology NAS。选择Domoticz for Synology DSM 6.1 with Python Plugin Beta。python plugin系统很重要,后面会用到。
下载的是.spk文件,这是Synology DSM系统的安装包。直接在Package Center中选择Manual Install即可。安装成功后如下图。

其他依赖包

Domoticz的python插件系统依赖于Python3
broadlink-http-rest server依赖于Python2
HA-Bridge Server依赖于 Java8
最好再装上git,方便checkout GitHub上的开源项目
所以这四个包都得安装,如下图

Broadlink into Domoticz

Domoticz Broadlink Python Plugin + Controlicz

这是本篇中最糟心的部分,陷阱无数。后面专录踩坑记录。
Domoticz Broadlink plugin和broadlink-http-rest均是基于python-broadlink。所以要先安装python-broadlink。

安装python-broadlink

在很多文章中python-broadlink都是用pip来安装,例如pip3 install broadlink。但是由于broadlink基于pycrypto,而安装pycrypto的时候会要在本地进行编译。Synology NAS上显然没有编译环境(GCC and etc.)。所以安装会失败。但是根据作者的描述,最新的python-broadlink并不基于pycrypto了,因为pycrypto已经被弃坑了。最新的python-broadlink基于pyaes库来实现加解密。所以正确的步骤是:

  • 确保使用python3
  • git clone python-broadlink
  • 切到python-broadlink目录,python setup.py install
  • python -m pip install pyaes因为Synology上无法安装pip3,这是变通的方法。使用python get-pip.py安装的仍然是pip2

安装Domoticz Python Plugin

参考链接3是插件作者提供的Domoiticz wiki页,不过全文基于Windows,参考其中others章节,仍然很好用。步骤如下:

  • 下载plugin files
  • Domoticz的插件目录在/usr/local/domoticz/var/plugins/BroadlinkRM2/,拷贝插件文件到该目录,包括:
    • plugin.py ——插件的主文件
    • plugin_send.py
    • plugin_http.py
    • plugin_http.sh
      这里需要注意插件目录不是/usr/local/domoticz/plugins/BroadlinkRM2/,虽然这个目录底下有example目录还有其他插件的目录,但当我把插件文件放在该目录下时,会出现插件未加载的情况。
  • 在Synology DSM的Package Center中重启Domoticz。注意后面每次修改plugin都需要重启Domoticz。
    至此安装就成功了,直接在Domoticz中添加BroadLink设备如图。

添加Broadlink Hardware

按照插件作者的步骤,下面应该可以添加Hardware,并可以Discover Broadlink设备,添加Hardware如下图:

导入易控(e-control)设备

参考链接3中也有详细描述。在User Guide->Inside Domoticz->xxx - import。可能作者使用的是非多字节语言,在处理导入文件时,若含有中文字,会出现失败的情况。参见踩坑记录中的解决方法。
另外还有一点要注意的是,在点击WebStart以后,Domoticz会开启一个运行于9000的web服务器,用以上传易控的设备文件(jsonButton, jsonIrCode, jsonSubIr)。要注意NAS的Firewall配置。

Controlicz

在搜索到的文章中鲜有提及。我也是在偶然的翻阅中发现了这个Alexa Skill。使用Controlicz就可以不用通过HA-Bridge来曲线救国了。但实际使用中发现还是各有利弊。后文讲完HA-Bridge章节,会提到两者的优缺点。
使用Controlicz的方法是:

  • 使用Alexa的账号来注册Controlicz
  • Alexa app上enable skill就好了
  • 对着Alexa说discover device

    注意:Controlcz中要求:

    • Domoticz服务必须外网可以访问,所以要配置家里的端口映射,以及DDNS,因为使用的是Synology,这个我早就已经有了
    • Controlicz要求Domoticz服务运行于HTTPS协议,所以必须使能8443端口,注意NAS的Firewall配置

踩坑记录

至此,如果能完成上面所有的步骤,对Alexa说”Turn on the TV”,应该已经会有正确响应了。但遗憾的是,中间还有一些东西要做:

Import Error “broadlink”

明明已经安装了python-broadlink,而且在REPL中也可以正确import,但是Domoticz log中始终报错。原因是PYTHONPATH不正确。在plugin.py中已有处理:
if sys.platform.startswith('linux'):
# linux specific code here
# doesn't work even if set dist-packages => site-packages
sys.path.append(os.path.dirname(os.__file__) + '/dist-packages')
elif sys.platform.startswith('darwin'):
# mac
sys.path.append(os.path.dirname(os.__file__) + '/site-packages')
elif sys.platform.startswith('win32'):
# win specific
sys.path.append(os.path.dirname(os.__file__) + '\site-packages')
正确的broadlink库路径是`/usr/local/lib/python3.5/site-packages/broadlink-0.5-py3.5.egg/broadlink/`。上面linux系统的路径是dist-package。不过即便改成site-packages,依然报同样的错误。解决方法简单明了,将broadlink目录拷到plugins目录下面即可

cp -r /usr/local/lib/python3.5/site-packages/broadlink-0.5-py3.5.egg/broadlink/ /usr/local/domoticz/var/plugins/BroadlinkRM2/

Error Connecting to Broadlink device

有两个GitHub issue链接可以看一看:
# broadlink/__init__.py
160 def encrypt_pyaes(self, payload):
161 aes = pyaes.AESModeOfOperationCBC(self.key, iv = bytes(self.iv))
162 return "".join([aes.encrypt(bytes(payload[i:i+16])) for i in range(0, len(payload), 16)]) # <==== Error is here

python3中bytes返回class bytes,而python2中返回字符串,如下:

Python2
>>> type(bytes([1,2,3]))
<type 'str'>

Python3
>>> type(bytes([1,2,3]))
<class 'bytes'>
>>> "".join([bytes([1,2]),bytes([2,3])]) <==== You cannot cat 2 bytes class objects as string
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: sequence item 0: expected str instance, bytes found
>>> b''.join([bytes([1,2]),bytes([2,3])]) <==== This is the good way
b'\x01\x02\x02\x03'

好了修改plugin.py对应的行就可以了。

Domoticz import文件中有中文的问题

在Python3中,如果文件有编码问题,例如包含中文,不能通过以下方式来读取文件并解码:

with open('jsonFile') as f:
textStr = f.read() <=== Python3 will complain about the codec error here.
textStr.decode('utf8')

正确的做法应当是

with open('jsonFile', encoding='utf8'):
textStr = f.read() <=== Everything here has already been unicode.

所以修改所有plugin.py中的open语句即可。
还有一处要修改的是针对ConfigParser的编码问题。如下是我修改好的:

config = configparser.ConfigParser()
config.read(path, encoding='utf8')

现在试一试,连接,发现,导入,应该都没问题了吧。

Domoticz + broadlink-http-rest + HA-Bridge

这个方案不需要借助Broadlink的Domoticz python插件,而是利用Domoticz中的Dummy Hardware,如下图:

安装HA-Bridge

HA-Bridge是将Alexa不支持的设备模拟成Alexa原生支持的Philips HUE Light。而HA-Bridge可以直接导入Domoticz中的所有设备。缺点是,他只能支持开关和调亮度的操作。
HA-Bridge是一个java的jar包,我们要将之作为service运行在Synology后台,方法参看Run as a service on Synology
其实就是运行命令nohup java -jar -Dserver.port=8085 /ha-bridge-4.5.6.jar &
注意放到Synology的计划任务里。

安装broadlink-http-rest

从GitHub上clone下broadlink-http-rest。然后和HA-Bridge一样,要以service的方式运行在后台。否则ssh连接一断开,server就退出了。命令为nohup python broadlink-http-rest/server.py &
注意要用nohup,否则serveer仍然会随着ssh断开而退出。

计划任务&防火墙

配置好防火墙:

  • 8085: HA-Bridge server
  • 8080: broadlink-http-rest server
  • 8443: Domoticz HTTPS service
  • 9000: Domoticz Broadlink web service

配置好计划任务:

Broadlink into Domoticz

在Setup->Hardware下建立Dummy Hardware,然后点击Create Virtual Sensors,然后在Switches tab里找到刚建好的virtual sensor,点击Edit,按下图配置好On Action和Off Action。

Domoticz into HA-Bridge

参见参考链接2,从Configuring HA bridge开始讲如何配置Domoticz到HA-Bridge service。然后就可以直接添加Domoticz里的设备了。

HA-Bridge vs. Controlicz

General Smart Home

Controlicz可以整合Domoticz所有设备到Alexa中,譬如,如果Alexa并不原生支持该smart home设备,则可以先在家里搭建Domoticz服务器,然后通过Controlicz将之整合到Alexa中。根据Controlicz官网support列表,其并不只局限于开关设备:

整合Broadlink

在整合Broadlink上,两者就各有胜场了。Broadlink是通用遥控器。所以通过Broadlink导入的设备都是按钮设备,每个按钮都是一个Push Button设备,所以只有一个触发的操作。这样也合理,因为按钮只有触发,并没有开状态或者关状态。这甚至不如灯的状态多。
例如:一台电风扇的开关按钮,按一下是开,再按一下是关。如果通过Controlicz直接整合Domoticz按钮设备,则要开电风扇,要对Alexa说“Turn on the Fan”,要关则还是说“Turn on the Fan”。是不是有点奇怪。
如果是HA-Bridge,针对开关状态,就可以说“Turn on the Fan”和“Turn off the Fan”。是不是好多了。
Controlicz整合Domoticz的好处就是,Domoticz Broadlink Python plugin支持直接一次性从易控导入已定义设备,可以充分发挥易控GUI的特点,省得一个一个设备自己定义了。

Broadlink原生Skill + IHC

在整完上面那一大堆之后,我突然发现原来Alexa已经有Broadlink的原生Skill了,但要配合最新版的Broadlink IHC app使用。不过令人糟心的是这两个app的评分都低的可怜。

  • IHC playstore 2.2分
  • Alexa Broadlink Skill 2.3分
    从官网的example看目前只支持TV。经我实践确实如此,添加了电视和空调,Alexa却只能discover到电视,没有空调。

参考链接

  1. 5 open source home automation tools
  2. Alex - Domoticz
  3. Domoticz Wiki Plugins/BroadlinkRM2
  4. #原创新人# 群晖安装broadlink-http-rest代替RMBridge_生活记录_什么值得买
%23Domoticz%20+%20BroadLink%20+%20Synology%20+%20Amazon%20Echo%u5B9E%u73B0%u667A%u80FD%u5BB6%u5C45%0A@%28myblog%29%5Bsynology%2C%20broadlink%2C%20domoticz%2C%20IoT%5D%0A%0A%23%23Overview%0A%u6B64%u4E8B%u7F18%u8D77%u4E8E%u5728%u7F8E%u5E1D%u4E70%u4E86Amazon%20Echo%uFF0C%u60F3%u7740%u56DE%u6765%u53EF%u4EE5%u5728%u5BB6%u91CC%u591A%u8BF4%u82F1%u8BED%uFF0C%u7ED9%u513F%u5B50%u4E00%u4E9B%u718F%u9676%uFF0C%u4E5F%u662F%u4E2A%u5927%u73A9%u5177%uFF0C%u8BF4%u4E0D%u5B9A%u4EBA%u5BB6%u4F1A%u7231%u4E0A%u5462%u3002%u540E%u6765%u60F3%u7740%u6574%u5929%u5BF9Alexa%u543C%u8FD9%u543C%u90A3%u7684%uFF0C%u5982%u679C%u80FD%u5524%u4ED6%u5E2E%u54B1%u5F00%u7A7A%u8C03%uFF0C%u5F00%u7535%u89C6%uFF0C%u5F00%u706F%uFF0C%u90A3%u5C31%u4E0D%u7528%u6574%u5929%u64CD%u5FC3%u9065%u63A7%u5668%u4E86%u5417%u3002%u5C24%u5176%uFF0C%u6574%u5929%u76EF%u7740%u9065%u63A7%u5668%u7684%u4E3B%uFF0C%u62CE%u7740%u9065%u63A7%u5668%u4E00%u6447%u4E00%u6446%u5C31%u8D70%u4E86%uFF0C%u7136%u540E%u6492%u7740%u6B22%u513F%u8DD1%u56DE%u6765%uFF0C%u9065%u63A7%u5668%u5C31%u4E0D%u77E5%u9053%u4E22%u54EA%u513F%u4E86%u3002%0A%u9996%u5148%u67E5%u5230%u7684%u662FBroadLink%u914D%u4EF6%uFF0C%u8FD9%u5176%u5B9E%u662F%u4E00%u4E2AWiFi%u8F6C%u7EA2%u5916+%u5C04%u9891%u7684%u914D%u4EF6%u3002%u6211%u4E70%u7684%u662F%5B%u8FD9%u4E2A%5D%28https%3A//detail.tmall.com/item.htm%3Fspm%3Da230r.1.14.8.ebb2eb2Gs49Vt%26id%3D43623542607%26cm_id%3D140105335569ed55e27b%26abbucket%3D13%26skuId%3D77095242268%29%u3002%u901A%u8FC7%u5176%u81EA%u5E26%u7684%u5B66%u4E60%u6A21%u5F0F%uFF0C%u53EF%u4EE5%u5B66%u4E60%u4EFB%u4F55%u4E00%u4E2A%u9065%u63A7%u5668%u7684%u9065%u63A7%u7801%u3002%u914D%u5408Broadlink%u81EA%u5BB6%u7684app%u5C31%u5DF2%u7ECF%u53EF%u4EE5%u5B9E%u73B0%u9065%u63A7%u5668%u7535%u5B50%u5316%u4E86%u3002Broadlink%u6709%u5B89%u5353%u4E0A%u6709%u4E24%u4E2Aapp%uFF1A%0A-%20%5Be-control%5D%28https%3A//play.google.com/store/apps/details%3Fid%3Dcom.broadlink.rmt%29%20%0A-%20%5BIHC%5D%28https%3A//play.google.com/store/apps/details%3Fid%3Dcn.com.broadlink.econtrol.plus%29%u53EA%u6709play%20store%u4E0A%u6709%0A%0A%u4E0D%u591F%u5B8C%u7F8E%u7684%u662F%u4ECD%u7136%u9700%u8981%u638F%u51FA%u4E2A%u624B%u673A%uFF0C%u7FFB%u627Eapp%uFF0C%u4E00%u5806%u4E71%u70B9%u3002%u8FD9%u4E0D%u662F%u6211%u60F3%u8981%u7684%u3002%0A%0A%u7EE7%u7EED%u767E%u5EA6%uFF0CGoogle%uFF0C%u5982%u679C%u8981%u6574%u5408Amazon%20Echo%u2014%u2014%u4E0B%u6587%u7B80%u79F0Alexa%u4EE5%u56FE%u65B9%u4FBF%uFF0C%u6709%u4EE5%u4E0B%u51E0%u79CD%u65B9%u6848%uFF1A%0A1.%20%u672C%u8EAB%u662F%u667A%u80FD%u5316%u5BB6%u5C45%uFF0C%u5E76%u4E14%u5382%u5BB6%u6709%u5BF9%u5E94%u7684Skill%uFF0Cenable%u8BE5Skill%u5373%u53EF%uFF0C%u597D%u5904%u662F%u5404%u79CD%u539F%u751F%u7684%u8BBE%u7F6E%uFF0C%u4F8B%u5982%3A%u7A7A%u8C03%u8BBE%u51E0%u5EA6%uFF0C%u706F%u5F00%u4EC0%u4E48%u989C%u8272%0A2.%20%u96C6%u6210BroadLink%uFF0C%u8BA9Alexa%u64CD%u4F5CBroadLink%uFF0C%u6B64%u65B9%u6848%u4ECD%u7136%u6709%u591A%u79CD%u5B50%u65B9%u6848%uFF1A%0A%091%29%20Android%20+%20%5BBroadlink%20RM%20Plugin%5D%28https%3A//play.google.com/store/apps/details%3Fid%3Dus.originally.tasker%29%20+%20Alexa%0A%092%29%20Domoticz/Home%20Assistant%20+%20%20%5BHA-Bridge%5D%28https%3A//github.com/bwssytems/ha-bridge%29%20+%20Alexa%0A%093%29%20Domoticz%20+%20python%20plugin%20+%20controlicz%20+%20Alexa%0A%094%29%20Broadlink%u539F%u751Fskill%20+%20Alexa%0A3.%20Domoticz/Home%20Assistant%20+%20HomeBridge%20+%20Siri%0A%0A%u5176%u4E2DDomoticz%u548CHome%20Assistant%u5747%u4E3AHome%20Automation%20System%uFF0C%u7F51%u4E0A%u4E24%u8005%u90FD%u6709%u5F88%u591A%u4EBA%u5B9E%u8DF5%uFF0C%u5404%u6709%u4F18%u52A3%u3002%u5728%u53C2%u8003%u94FE%u63A5%u4E2D%u4E13%u95E8%u5BF95%u79CD%u4E0D%u540C%u7684%u7CFB%u7EDF%u8FDB%u884C%u4E86%u6BD4%u8F83%u3002%u56E0%u4E3A%u6211%u6709Synology%20NAS%uFF0C%u53C8%u53EF%u4EE5%u539F%u751F%u96C6%u6210%uFF0C%u6240%u4EE5%u6BEB%u4E0D%u72B9%u8C6B%u9009%u62E9%u4E86Domoticz%u3002%u5145%u5206%u53D1%u6325NAS%207*24%u5C0F%u65F6%u4E0D%u4E0B%u7EBF%u7684%u4F18%u52BF%uFF0C%u5B9E%u73B0%u667A%u80FD%u5BB6%u5EAD%u7F51%u5173%u3002%u9488%u5BF9%u7B2C%u4E8C%u70B9%u4E2D%u7684%u540E3%u4E2A%282-4%29%u65B9%u6848%u5168%u90E8%u8FDB%u884C%u4E86%u5B9E%u73B0%u3002%u540E%u9762%u4F1A%u4E00%u4E00%u5217%u8FF0%u3002%u6700%u7EC8%u65B9%u6848%u67B6%u6784%u5982%u4E0B%u56FE%uFF1A%0A%21%5BAlt%20text%5D%28./1501339487875.png%29%0A%0A%23%23%20Domoticz%20into%20Synology%0A%u8FD9%u4E2A%u8D85%u7EA7%u7B80%u5355%u3002%u6709%u5B98%u65B9%u652F%u6301%5BDomoticz%20for%20Synology%20NAS%5D%28http%3A//www.jadahl.com/%29%u3002%u9009%u62E9%5BDomoticz%20for%20Synology%20DSM%206.1%20with%20Python%20Plugin%20Beta%5D%28http%3A//www.jadahl.com/domoticz_beta_6.1_python/%29%u3002python%20plugin%u7CFB%u7EDF%u5F88%u91CD%u8981%uFF0C%u540E%u9762%u4F1A%u7528%u5230%u3002%0A%u4E0B%u8F7D%u7684%u662F.spk%u6587%u4EF6%uFF0C%u8FD9%u662FSynology%20DSM%u7CFB%u7EDF%u7684%u5B89%u88C5%u5305%u3002%u76F4%u63A5%u5728Package%20Center%u4E2D%u9009%u62E9Manual%20Install%u5373%u53EF%u3002%u5B89%u88C5%u6210%u529F%u540E%u5982%u4E0B%u56FE%u3002%0A%21%5BAlt%20text%5D%28./1501340457645.png%29%0A%0A%23%23%20%u5176%u4ED6%u4F9D%u8D56%u5305%0ADomoticz%u7684python%u63D2%u4EF6%u7CFB%u7EDF%u4F9D%u8D56%u4E8EPython3%0Abroadlink-http-rest%20server%u4F9D%u8D56%u4E8EPython2%0A%5BHA-Bridge%20Server%5D%28https%3A//github.com/bwssytems/ha-bridge%29%u4F9D%u8D56%u4E8E%20Java8%0A%u6700%u597D%u518D%u88C5%u4E0Agit%uFF0C%u65B9%u4FBFcheckout%20GitHub%u4E0A%u7684%u5F00%u6E90%u9879%u76EE%0A%u6240%u4EE5%u8FD9%u56DB%u4E2A%u5305%u90FD%u5F97%u5B89%u88C5%uFF0C%u5982%u4E0B%u56FE%0A%21%5BAlt%20text%5D%28./1501340796886.png%29%21%5BAlt%20text%5D%28./1501340823935.png%29%21%5BAlt%20text%5D%28./1501340847466.png%29%0A%0A%23%23%20Broadlink%20into%20Domoticz%0A%0A%23%23%23%20Domoticz%20Broadlink%20Python%20Plugin%20+%20Controlicz%0A%u8FD9%u662F%u672C%u7BC7%u4E2D%u6700%u7CDF%u5FC3%u7684%u90E8%u5206%uFF0C%u9677%u9631%u65E0%u6570%u3002%u540E%u9762%u4E13%u5F55%u8E29%u5751%u8BB0%u5F55%u3002%0ADomoticz%20Broadlink%20plugin%u548C%5Bbroadlink-http-rest%5D%28https%3A//github.com/radinsky/broadlink-http-rest%29%u5747%u662F%u57FA%u4E8E%5Bpython-broadlink%5D%28https%3A//github.com/mjg59/python-broadlink%29%u3002%u6240%u4EE5%u8981%u5148%u5B89%u88C5python-broadlink%u3002%0A%0A%23%23%23%23%20%u5B89%u88C5python-broadlink%0A%u5728%u5F88%u591A%u6587%u7AE0%u4E2Dpython-broadlink%u90FD%u662F%u7528pip%u6765%u5B89%u88C5%uFF0C%u4F8B%u5982%60pip3%20install%20broadlink%60%u3002%u4F46%u662F%u7531%u4E8Ebroadlink%u57FA%u4E8Epycrypto%uFF0C%u800C%u5B89%u88C5pycrypto%u7684%u65F6%u5019%u4F1A%u8981%u5728%u672C%u5730%u8FDB%u884C%u7F16%u8BD1%u3002Synology%20NAS%u4E0A%u663E%u7136%u6CA1%u6709%u7F16%u8BD1%u73AF%u5883%28GCC%20and%20etc.%29%u3002%u6240%u4EE5%u5B89%u88C5%u4F1A%u5931%u8D25%u3002%u4F46%u662F%u6839%u636E%u4F5C%u8005%u7684%u63CF%u8FF0%uFF0C%u6700%u65B0%u7684python-broadlink%u5E76%u4E0D%u57FA%u4E8Epycrypto%u4E86%uFF0C%u56E0%u4E3Apycrypto%u5DF2%u7ECF%u88AB%u5F03%u5751%u4E86%u3002%u6700%u65B0%u7684python-broadlink%u57FA%u4E8Epyaes%u5E93%u6765%u5B9E%u73B0%u52A0%u89E3%u5BC6%u3002%u6240%u4EE5%u6B63%u786E%u7684%u6B65%u9AA4%u662F%uFF1A%0A-%20%u786E%u4FDD%u4F7F%u7528python3%0A-%20git%20clone%20python-broadlink%0A-%20%u5207%u5230python-broadlink%u76EE%u5F55%uFF0C%60python%20setup.py%20install%60%0A-%20%60python%20-m%20pip%20install%20pyaes%60%u56E0%u4E3ASynology%u4E0A%u65E0%u6CD5%u5B89%u88C5pip3%uFF0C%u8FD9%u662F%u53D8%u901A%u7684%u65B9%u6CD5%u3002%u4F7F%u7528%60python%20get-pip.py%60%u5B89%u88C5%u7684%u4ECD%u7136%u662Fpip2%0A%0A%23%23%23%23%20%u5B89%u88C5Domoticz%20Python%20Plugin%0A%u53C2%u8003%u94FE%u63A53%u662F%u63D2%u4EF6%u4F5C%u8005%u63D0%u4F9B%u7684Domoiticz%20wiki%u9875%uFF0C%u4E0D%u8FC7%u5168%u6587%u57FA%u4E8EWindows%uFF0C%u53C2%u8003%u5176%u4E2Dothers%u7AE0%u8282%uFF0C%u4ECD%u7136%u5F88%u597D%u7528%u3002%u6B65%u9AA4%u5982%u4E0B%uFF1A%0A-%20%u4E0B%u8F7D%5Bplugin%20files%5D%28https%3A//www.dropbox.com/sh/htyghey9e402u4y/AACeb1cXqaPd9gBVl5TL3H36a%3Fdl%3D0%29%0A-%20Domoticz%u7684%u63D2%u4EF6%u76EE%u5F55%u5728%60/usr/local/domoticz/var/plugins/BroadlinkRM2/%60%uFF0C%u62F7%u8D1D%u63D2%u4EF6%u6587%u4EF6%u5230%u8BE5%u76EE%u5F55%uFF0C%u5305%u62EC%uFF1A%0A%09-%20plugin.py%20%u2014%u2014%u63D2%u4EF6%u7684%u4E3B%u6587%u4EF6%0A%09-%20plugin_send.py%0A%09-%20plugin_http.py%0A%09-%20plugin_http.sh%0A%09%u8FD9%u91CC%u9700%u8981%u6CE8%u610F%u63D2%u4EF6%u76EE%u5F55%u4E0D%u662F%60/usr/local/domoticz/plugins/BroadlinkRM2/%60%uFF0C%u867D%u7136%u8FD9%u4E2A%u76EE%u5F55%u5E95%u4E0B%u6709example%u76EE%u5F55%u8FD8%u6709%u5176%u4ED6%u63D2%u4EF6%u7684%u76EE%u5F55%uFF0C%u4F46%u5F53%u6211%u628A%u63D2%u4EF6%u6587%u4EF6%u653E%u5728%u8BE5%u76EE%u5F55%u4E0B%u65F6%uFF0C%u4F1A%u51FA%u73B0%u63D2%u4EF6%u672A%u52A0%u8F7D%u7684%u60C5%u51B5%u3002%0A-%20%u5728Synology%20DSM%u7684Package%20Center%u4E2D%u91CD%u542FDomoticz%u3002%u6CE8%u610F%u540E%u9762%u6BCF%u6B21%u4FEE%u6539plugin%u90FD%u9700%u8981%u91CD%u542FDomoticz%u3002%0A%u81F3%u6B64%u5B89%u88C5%u5C31%u6210%u529F%u4E86%uFF0C%u76F4%u63A5%u5728Domoticz%u4E2D%u6DFB%u52A0BroadLink%u8BBE%u5907%u5982%u56FE%u3002%0A%21%5BAlt%20text%5D%28./1500861858610.png%29%0A%0A%23%23%23%23%20%u6DFB%u52A0Broadlink%20Hardware%0A%u6309%u7167%u63D2%u4EF6%u4F5C%u8005%u7684%u6B65%u9AA4%uFF0C%u4E0B%u9762%u5E94%u8BE5%u53EF%u4EE5%u6DFB%u52A0Hardware%uFF0C%u5E76%u53EF%u4EE5Discover%20Broadlink%u8BBE%u5907%uFF0C%u6DFB%u52A0Hardware%u5982%u4E0B%u56FE%uFF1A%0A%21%5BAlt%20text%5D%28./1501368649742.png%29%0AIP%u53EF%u4EE5%u8BBE%u4E3A127.0.0.1%uFF0CMAC%u8BBE%u4E3A%u51680%uFF0C%u7136%u540E%u53EF%u4EE5%u901A%u8FC7%u63D2%u4EF6%u63D0%u4F9B%u7684%u53D1%u73B0%u529F%u80FD%u6765%u8FDE%u63A5broadlink%u8BBE%u5907%u3002%u53C2%u8003%u94FE%u63A53%u4E2D%u5747%u6709%u8BE6%u7EC6%u63CF%u8FF0%u3002%u4F46%u5B9E%u9645%u5E76%u4E0D%u90A3%u4E48%u987A%u5229%uFF0C%u53C2%u8003%u4E0B%u9762%u8E29%u5751%u8BB0%u5F55%u3002%0A%0A%0A%23%23%23%23%20%u5BFC%u5165%u6613%u63A7%28e-control%29%u8BBE%u5907%0A%u53C2%u8003%u94FE%u63A53%u4E2D%u4E5F%u6709%u8BE6%u7EC6%u63CF%u8FF0%u3002%u5728User%20Guide-%3EInside%20Domoticz-%3Exxx%20-%20import%u3002%u53EF%u80FD%u4F5C%u8005%u4F7F%u7528%u7684%u662F%u975E%u591A%u5B57%u8282%u8BED%u8A00%uFF0C%u5728%u5904%u7406%u5BFC%u5165%u6587%u4EF6%u65F6%uFF0C%u82E5%u542B%u6709%u4E2D%u6587%u5B57%uFF0C%u4F1A%u51FA%u73B0%u5931%u8D25%u7684%u60C5%u51B5%u3002%u53C2%u89C1%u8E29%u5751%u8BB0%u5F55%u4E2D%u7684%u89E3%u51B3%u65B9%u6CD5%u3002%0A%u53E6%u5916%u8FD8%u6709%u4E00%u70B9%u8981%u6CE8%u610F%u7684%u662F%uFF0C%u5728%u70B9%u51FBWebStart%u4EE5%u540E%uFF0CDomoticz%u4F1A%u5F00%u542F%u4E00%u4E2A%u8FD0%u884C%u4E8E9000%u7684web%u670D%u52A1%u5668%uFF0C%u7528%u4EE5%u4E0A%u4F20%u6613%u63A7%u7684%u8BBE%u5907%u6587%u4EF6%28jsonButton%2C%20jsonIrCode%2C%20jsonSubIr%29%u3002%u8981%u6CE8%u610FNAS%u7684Firewall%u914D%u7F6E%u3002%0A%0A%23%23%23%20Controlicz%0A%u5728%u641C%u7D22%u5230%u7684%u6587%u7AE0%u4E2D%u9C9C%u6709%u63D0%u53CA%u3002%u6211%u4E5F%u662F%u5728%u5076%u7136%u7684%u7FFB%u9605%u4E2D%u53D1%u73B0%u4E86%u8FD9%u4E2AAlexa%20Skill%u3002%u4F7F%u7528Controlicz%u5C31%u53EF%u4EE5%u4E0D%u7528%u901A%u8FC7HA-Bridge%u6765%u66F2%u7EBF%u6551%u56FD%u4E86%u3002%u4F46%u5B9E%u9645%u4F7F%u7528%u4E2D%u53D1%u73B0%u8FD8%u662F%u5404%u6709%u5229%u5F0A%u3002%u540E%u6587%u8BB2%u5B8CHA-Bridge%u7AE0%u8282%uFF0C%u4F1A%u63D0%u5230%u4E24%u8005%u7684%u4F18%u7F3A%u70B9%u3002%0A%u4F7F%u7528Controlicz%u7684%u65B9%u6CD5%u662F%uFF1A%0A-%20%u4F7F%u7528Alexa%u7684%u8D26%u53F7%u6765%u6CE8%u518C%5BControlicz%5D%28https%3A//www.controlicz.com/%29%0A-%20Alexa%20app%u4E0Aenable%20skill%u5C31%u597D%u4E86%0A-%20%u5BF9%u7740Alexa%u8BF4discover%20device%0A%3E%u6CE8%u610F%uFF1AControlcz%u4E2D%u8981%u6C42%uFF1A%0A%3E-%20Domoticz%u670D%u52A1%u5FC5%u987B%u5916%u7F51%u53EF%u4EE5%u8BBF%u95EE%uFF0C%u6240%u4EE5%u8981%u914D%u7F6E%u5BB6%u91CC%u7684%u7AEF%u53E3%u6620%u5C04%uFF0C%u4EE5%u53CADDNS%uFF0C%u56E0%u4E3A%u4F7F%u7528%u7684%u662FSynology%uFF0C%u8FD9%u4E2A%u6211%u65E9%u5C31%u5DF2%u7ECF%u6709%u4E86%0A%3E-%20Controlicz%u8981%u6C42Domoticz%u670D%u52A1%u8FD0%u884C%u4E8EHTTPS%u534F%u8BAE%uFF0C%u6240%u4EE5%u5FC5%u987B%u4F7F%u80FD8443%u7AEF%u53E3%uFF0C%u6CE8%u610FNAS%u7684Firewall%u914D%u7F6E%0A%0A%23%23%23%20%u8E29%u5751%u8BB0%u5F55%0A%u81F3%u6B64%uFF0C%u5982%u679C%u80FD%u5B8C%u6210%u4E0A%u9762%u6240%u6709%u7684%u6B65%u9AA4%uFF0C%u5BF9Alexa%u8BF4%22Turn%20on%20the%20TV%22%uFF0C%u5E94%u8BE5%u5DF2%u7ECF%u4F1A%u6709%u6B63%u786E%u54CD%u5E94%u4E86%u3002%u4F46%u9057%u61BE%u7684%u662F%uFF0C%u4E2D%u95F4%u8FD8%u6709%u4E00%u4E9B%u4E1C%u897F%u8981%u505A%uFF1A%0A%23%23%23%23%20Import%20Error%20%22broadlink%22%0A%u660E%u660E%u5DF2%u7ECF%u5B89%u88C5%u4E86python-broadlink%uFF0C%u800C%u4E14%u5728REPL%u4E2D%u4E5F%u53EF%u4EE5%u6B63%u786Eimport%uFF0C%u4F46%u662FDomoticz%20log%u4E2D%u59CB%u7EC8%u62A5%u9519%u3002%u539F%u56E0%u662FPYTHONPATH%u4E0D%u6B63%u786E%u3002%u5728plugin.py%u4E2D%u5DF2%u6709%u5904%u7406%uFF1A%0A%60%60%60%0Aif%20sys.platform.startswith%28%27linux%27%29%3A%0A%20%20%20%20%23%20linux%20specific%20code%20here%0A%20%20%20%20%23%20doesn%27t%20work%20even%20if%20set%20dist-packages%20%3D%3E%20site-packages%0A%20%20%20%20sys.path.append%28os.path.dirname%28os.__file__%29%20+%20%27/dist-packages%27%29%0Aelif%20sys.platform.startswith%28%27darwin%27%29%3A%0A%20%20%20%20%23%20mac%0A%20%20%20%20sys.path.append%28os.path.dirname%28os.__file__%29%20+%20%27/site-packages%27%29%0Aelif%20sys.platform.startswith%28%27win32%27%29%3A%0A%20%20%20%20%23%20%20win%20specific%0A%20%20%20%20sys.path.append%28os.path.dirname%28os.__file__%29%20+%20%27%5Csite-packages%27%29%0A%60%60%60%0A%u6B63%u786E%u7684broadlink%u5E93%u8DEF%u5F84%u662F%60/usr/local/lib/python3.5/site-packages/broadlink-0.5-py3.5.egg/broadlink/%60%u3002%u4E0A%u9762linux%u7CFB%u7EDF%u7684%u8DEF%u5F84%u662Fdist-package%u3002%u4E0D%u8FC7%u5373%u4FBF%u6539%u6210site-packages%uFF0C%u4F9D%u7136%u62A5%u540C%u6837%u7684%u9519%u8BEF%u3002%u89E3%u51B3%u65B9%u6CD5%u7B80%u5355%u660E%u4E86%uFF0C%u5C06broadlink%u76EE%u5F55%u62F7%u5230plugins%u76EE%u5F55%u4E0B%u9762%u5373%u53EF%0A%3Ecp%20-r%20/usr/local/lib/python3.5/site-packages/broadlink-0.5-py3.5.egg/broadlink/%20/usr/local/domoticz/var/plugins/BroadlinkRM2/%0A%0A%23%23%23%23%20Error%20Connecting%20to%20Broadlink%20device%0A%u6709%u4E24%u4E2AGitHub%20issue%u94FE%u63A5%u53EF%u4EE5%u770B%u4E00%u770B%uFF1A%0A-%20%5BError%20Connecting%20to%20Broadlink%20device%20%23107%5D%28https%3A//github.com/mjg59/python-broadlink/issues/107%29%0A-%20%5Blpad%20fix%20broken%3F%20%2397%5D%28https%3A//github.com/mjg59/python-broadlink/issues/97%29%0A%u6216%u8BB8%u4F1A%u6709%u5E2E%u52A9%uFF0C%u4F46%u5BF9%u4E8E%u6211%u7684%u95EE%u9898%u4E0D%u662F%u6B63%u89E3%u3002%0ARoot%20cause%u662Fbytes%u4F7F%u7528%u65B9%u6CD5%u4E0D%u5BF9%uFF0C%u9519%u8BEF%u7684%u5730%u65B9%u5728%u8FD9%u91CC%0A%60%60%60%0A%23%20broadlink/__init__.py%0A160%20%20%20def%20encrypt_pyaes%28self%2C%20payload%29%3A%0A161%20%20%20%20%20aes%20%3D%20pyaes.AESModeOfOperationCBC%28self.key%2C%20iv%20%3D%20bytes%28self.iv%29%29%0A162%20%20%20%20%20return%20%22%22.join%28%5Baes.encrypt%28bytes%28payload%5Bi%3Ai+16%5D%29%29%20for%20i%20in%20range%280%2C%20len%28payload%29%2C%2016%29%5D%29%20%23%20%3C%3D%3D%3D%3D%20Error%20is%20here%0A%60%60%60%0Apython3%u4E2Dbytes%u8FD4%u56DEclass%20bytes%uFF0C%u800Cpython2%u4E2D%u8FD4%u56DE%u5B57%u7B26%u4E32%uFF0C%u5982%u4E0B%uFF1A%0A%60%60%60%0APython2%0A%3E%3E%3E%20type%28bytes%28%5B1%2C2%2C3%5D%29%29%0A%3Ctype%20%27str%27%3E%0A%0APython3%0A%3E%3E%3E%20type%28bytes%28%5B1%2C2%2C3%5D%29%29%0A%3Cclass%20%27bytes%27%3E%0A%3E%3E%3E%20%22%22.join%28%5Bbytes%28%5B1%2C2%5D%29%2Cbytes%28%5B2%2C3%5D%29%5D%29%20%20%3C%3D%3D%3D%3D%20You%20cannot%20cat%202%20bytes%20class%20objects%20as%20string%0ATraceback%20%28most%20recent%20call%20last%29%3A%0A%20%20File%20%22%3Cstdin%3E%22%2C%20line%201%2C%20in%20%3Cmodule%3E%0ATypeError%3A%20sequence%20item%200%3A%20expected%20str%20instance%2C%20bytes%20found%0A%3E%3E%3E%20b%27%27.join%28%5Bbytes%28%5B1%2C2%5D%29%2Cbytes%28%5B2%2C3%5D%29%5D%29%20%3C%3D%3D%3D%3D%20This%20is%20the%20good%20way%0Ab%27%5Cx01%5Cx02%5Cx02%5Cx03%27%0A%60%60%60%0A%u597D%u4E86%u4FEE%u6539plugin.py%u5BF9%u5E94%u7684%u884C%u5C31%u53EF%u4EE5%u4E86%u3002%0A%0A%23%23%23%23%20Domoticz%20import%u6587%u4EF6%u4E2D%u6709%u4E2D%u6587%u7684%u95EE%u9898%0A%u5728Python3%u4E2D%uFF0C%u5982%u679C%u6587%u4EF6%u6709%u7F16%u7801%u95EE%u9898%uFF0C%u4F8B%u5982%u5305%u542B%u4E2D%u6587%uFF0C%u4E0D%u80FD%u901A%u8FC7%u4EE5%u4E0B%u65B9%u5F0F%u6765%u8BFB%u53D6%u6587%u4EF6%u5E76%u89E3%u7801%uFF1A%0A%60%60%60%0Awith%20open%28%27jsonFile%27%29%20as%20f%3A%0A%09textStr%20%3D%20f.read%28%29%20%3C%3D%3D%3D%20Python3%20will%20complain%20about%20the%20codec%20error%20here.%0A%09textStr.decode%28%27utf8%27%29%0A%60%60%60%0A%u6B63%u786E%u7684%u505A%u6CD5%u5E94%u5F53%u662F%0A%60%60%60%0Awith%20open%28%27jsonFile%27%2C%20encoding%3D%27utf8%27%29%3A%0A%09textStr%20%3D%20f.read%28%29%20%3C%3D%3D%3D%20Everything%20here%20has%20already%20been%20unicode.%0A%60%60%60%0A%u6240%u4EE5%u4FEE%u6539%u6240%u6709plugin.py%u4E2D%u7684open%u8BED%u53E5%u5373%u53EF%u3002%0A%u8FD8%u6709%u4E00%u5904%u8981%u4FEE%u6539%u7684%u662F%u9488%u5BF9ConfigParser%u7684%u7F16%u7801%u95EE%u9898%u3002%u5982%u4E0B%u662F%u6211%u4FEE%u6539%u597D%u7684%uFF1A%0A%60%60%60%0Aconfig%20%3D%20configparser.ConfigParser%28%29%0Aconfig.read%28path%2C%20encoding%3D%27utf8%27%29%0A%60%60%60%0A%u73B0%u5728%u8BD5%u4E00%u8BD5%uFF0C%u8FDE%u63A5%uFF0C%u53D1%u73B0%uFF0C%u5BFC%u5165%uFF0C%u5E94%u8BE5%u90FD%u6CA1%u95EE%u9898%u4E86%u5427%u3002%0A%0A%23%23%23%20Domoticz%20+%20broadlink-http-rest%20+%20HA-Bridge%0A%u8FD9%u4E2A%u65B9%u6848%u4E0D%u9700%u8981%u501F%u52A9Broadlink%u7684Domoticz%20python%u63D2%u4EF6%uFF0C%u800C%u662F%u5229%u7528Domoticz%u4E2D%u7684Dummy%20Hardware%uFF0C%u5982%u4E0B%u56FE%uFF1A%0A%21%5BAlt%20text%5D%28./1501376022176.png%29%0A%0A%23%23%23%23%20%u5B89%u88C5HA-Bridge%0AHA-Bridge%u662F%u5C06Alexa%u4E0D%u652F%u6301%u7684%u8BBE%u5907%u6A21%u62DF%u6210Alexa%u539F%u751F%u652F%u6301%u7684Philips%20HUE%20Light%u3002%u800CHA-Bridge%u53EF%u4EE5%u76F4%u63A5%u5BFC%u5165Domoticz%u4E2D%u7684%u6240%u6709%u8BBE%u5907%u3002%u7F3A%u70B9%u662F%uFF0C%u4ED6%u53EA%u80FD%u652F%u6301%u5F00%u5173%u548C%u8C03%u4EAE%u5EA6%u7684%u64CD%u4F5C%u3002%0AHA-Bridge%u662F%u4E00%u4E2Ajava%u7684jar%u5305%uFF0C%u6211%u4EEC%u8981%u5C06%u4E4B%u4F5C%u4E3Aservice%u8FD0%u884C%u5728Synology%u540E%u53F0%uFF0C%u65B9%u6CD5%u53C2%u770B%5BRun%20as%20a%20service%20on%20Synology%5D%28https%3A//github.com/bwssytems/ha-bridge/issues/310%29%u3002%0A%u5176%u5B9E%u5C31%u662F%u8FD0%u884C%u547D%u4EE4%60nohup%20java%20-jar%20-Dserver.port%3D8085%20/ha-bridge-4.5.6.jar%20%26%60%0A%u6CE8%u610F%u653E%u5230Synology%u7684%u8BA1%u5212%u4EFB%u52A1%u91CC%u3002%0A%0A%23%23%23%23%20%u5B89%u88C5broadlink-http-rest%0A%u4ECEGitHub%u4E0Aclone%u4E0B%5Bbroadlink-http-rest%5D%28https%3A//github.com/radinsky/broadlink-http-rest%29%u3002%u7136%u540E%u548CHA-Bridge%u4E00%u6837%uFF0C%u8981%u4EE5service%u7684%u65B9%u5F0F%u8FD0%u884C%u5728%u540E%u53F0%u3002%u5426%u5219ssh%u8FDE%u63A5%u4E00%u65AD%u5F00%uFF0Cserver%u5C31%u9000%u51FA%u4E86%u3002%u547D%u4EE4%u4E3A%60nohup%20python%20broadlink-http-rest/server.py%20%26%60%0A%u6CE8%u610F%u8981%u7528nohup%uFF0C%u5426%u5219serveer%u4ECD%u7136%u4F1A%u968F%u7740ssh%u65AD%u5F00%u800C%u9000%u51FA%u3002%0A%0A%23%23%23%23%20%u8BA1%u5212%u4EFB%u52A1%26%u9632%u706B%u5899%0A%u914D%u7F6E%u597D%u9632%u706B%u5899%uFF1A%0A%21%5BAlt%20text%5D%28./1501376973009.png%29%0A-%208085%3A%20HA-Bridge%20server%0A-%208080%3A%20broadlink-http-rest%20server%0A-%208443%3A%20Domoticz%20HTTPS%20service%0A-%209000%3A%20Domoticz%20Broadlink%20web%20service%0A%0A%u914D%u7F6E%u597D%u8BA1%u5212%u4EFB%u52A1%uFF1A%0A%21%5BAlt%20text%5D%28./1501377068095.png%29%0A%0A%23%23%23%23%20Broadlink%20into%20Domoticz%0A%u5728Setup-%3EHardware%u4E0B%u5EFA%u7ACBDummy%20Hardware%uFF0C%u7136%u540E%u70B9%u51FBCreate%20Virtual%20Sensors%uFF0C%u7136%u540E%u5728Switches%20tab%u91CC%u627E%u5230%u521A%u5EFA%u597D%u7684virtual%20sensor%uFF0C%u70B9%u51FBEdit%uFF0C%u6309%u4E0B%u56FE%u914D%u7F6E%u597DOn%20Action%u548COff%20Action%u3002%0A%21%5BAlt%20text%5D%28./1501376159204.png%29%0A%u8FD9%u91CCon%20off%20action%u5C31%u662F%u5229%u7528broadlink-http-rest%20service%u6765%u63A7%u5236broadlink%u5B66%u4E60%uFF0C%u5E76%u53D1%u9001%u547D%u4EE4%u5230broadlink%u6765%u5B9E%u73B0%u9065%u63A7%u529F%u80FD%u3002%u4F8B%u5982%uFF1Ahttp%3A//192.168.1.2%3A8080/learnCommand/kogntiaokai%20%u540Ebroadlink%20rm%u4F1A%u8FDB%u5165%u5B66%u4E60%u72B6%u6001%uFF0C%u4EAE%u9EC4%u706F%u3002%u5BF9%u7740%u6309%u9065%u63A7%u5668%u6309%u94AE%u5C31%u5B66%u4E60%u4E86%u3002%u8FD9%u91CCkongtiaokai%uFF08%u7A7A%u8C03%u5F00%uFF09%u662F%u81EA%u5DF1%u5B9A%u4E49%u7684%u9065%u63A7%u540D%u79F0%uFF0C%u5B66%u4E60%u4E0B%u4E00%u4E2A%u547D%u4EE4%u65F6%u628A%u8FD9%u4E2A%u5730%u65B9%u4FEE%u6539%u4E00%u4E0B%u5C31%u884C%u3002%u5B66%u4E60%u5B8C%u540E%u53EF%u4EE5%u5728%u6D4F%u89C8%u5668%u91CC%u9762%u8F93%u5165%20http%3A//192.168.1.2%3A8080/sendCommand/kongtiaokai%20%u6765%u8BA9broadlink%u53D1%u9001%u521A%u624D%u5B66%u4E60%u7684%u7EA2%u5916%u547D%u4EE4%uFF0C%u6765%u6D4B%u8BD5%u4E0B%u662F%u5426%u5B66%u4E60%u6210%u529F%u3002%0A%0A%23%23%23%23%20Domoticz%20into%20HA-Bridge%0A%u53C2%u89C1%u53C2%u8003%u94FE%u63A52%uFF0C%u4ECEConfiguring%20HA%20bridge%u5F00%u59CB%u8BB2%u5982%u4F55%u914D%u7F6EDomoticz%u5230HA-Bridge%20service%u3002%u7136%u540E%u5C31%u53EF%u4EE5%u76F4%u63A5%u6DFB%u52A0Domoticz%u91CC%u7684%u8BBE%u5907%u4E86%u3002%0A%21%5BAlt%20text%5D%28./1501401044317.png%29%0A%u6DFB%u52A0%u5B8C%u5C31%u662F%u8FD9%u6837%u5566%0A%21%5BAlt%20text%5D%28./1501403754697.png%29%0A%u6B64%u65F6%u5C31%u53EF%u4EE5%u901A%u8FC7Alexa%20discover%u4E86%u3002%u7136%u540E%u53EF%u4EE5%u7528Alexa%20turn%20on/off%u8BBE%u5907%u4E86%u3002%0A%0A%23%23%23%23%20HA-Bridge%20vs.%20Controlicz%0A%23%23%23%23%23%20General%20Smart%20Home%0AControlicz%u53EF%u4EE5%u6574%u5408Domoticz%u6240%u6709%u8BBE%u5907%u5230Alexa%u4E2D%uFF0C%u8B6C%u5982%uFF0C%u5982%u679CAlexa%u5E76%u4E0D%u539F%u751F%u652F%u6301%u8BE5smart%20home%u8BBE%u5907%uFF0C%u5219%u53EF%u4EE5%u5148%u5728%u5BB6%u91CC%u642D%u5EFADomoticz%u670D%u52A1%u5668%uFF0C%u7136%u540E%u901A%u8FC7Controlicz%u5C06%u4E4B%u6574%u5408%u5230Alexa%u4E2D%u3002%u6839%u636EControlicz%u5B98%u7F51support%u5217%u8868%uFF0C%u5176%u5E76%u4E0D%u53EA%u5C40%u9650%u4E8E%u5F00%u5173%u8BBE%u5907%uFF1A%0A%21%5BAlt%20text%5D%28./1501404140819.png%29%0AHA-Bridge%u56E0%u4E3A%u662F%u5C06%u8BBE%u5907%u6A21%u62DF%u6210Philips%20HUE%uFF0C%u6240%u4EE5%u5BF9Alexa%u6765%u8BF4%uFF0C%u8FD9%u4E9B%u8BBE%u5907%u5C31%u53EA%u662F%u4E00%u4E2APhilips%20HUE%20Light%uFF0C%u6240%u4EE5%u53EA%u80FD%u6709%u5F00%u5173%u548C%u8C03%u4EAE%u5EA6%u4E09%u79CD%u52A8%u4F5C%u3002%0A%u4ECE%u8FD9%u4E2A%u89D2%u5EA6%u8BF4%uFF0C%u4E24%u8005%u5E76%u65E0%u53EF%u6BD4%u6027%uFF0CControlicz%u5B8C%u80DC%0A%23%23%23%23%23%20%u6574%u5408Broadlink%0A%u5728%u6574%u5408Broadlink%u4E0A%uFF0C%u4E24%u8005%u5C31%u5404%u6709%u80DC%u573A%u4E86%u3002Broadlink%u662F%u901A%u7528%u9065%u63A7%u5668%u3002%u6240%u4EE5%u901A%u8FC7Broadlink%u5BFC%u5165%u7684%u8BBE%u5907%u90FD%u662F%u6309%u94AE%u8BBE%u5907%uFF0C%u6BCF%u4E2A%u6309%u94AE%u90FD%u662F%u4E00%u4E2APush%20Button%u8BBE%u5907%uFF0C%u6240%u4EE5%u53EA%u6709%u4E00%u4E2A%u89E6%u53D1%u7684%u64CD%u4F5C%u3002%u8FD9%u6837%u4E5F%u5408%u7406%uFF0C%u56E0%u4E3A%u6309%u94AE%u53EA%u6709%u89E6%u53D1%uFF0C%u5E76%u6CA1%u6709%u5F00%u72B6%u6001%u6216%u8005%u5173%u72B6%u6001%u3002%u8FD9%u751A%u81F3%u4E0D%u5982%u706F%u7684%u72B6%u6001%u591A%u3002%0A%u4F8B%u5982%uFF1A%u4E00%u53F0%u7535%u98CE%u6247%u7684%u5F00%u5173%u6309%u94AE%uFF0C%u6309%u4E00%u4E0B%u662F%u5F00%uFF0C%u518D%u6309%u4E00%u4E0B%u662F%u5173%u3002%u5982%u679C%u901A%u8FC7Controlicz%u76F4%u63A5%u6574%u5408Domoticz%u6309%u94AE%u8BBE%u5907%uFF0C%u5219%u8981%u5F00%u7535%u98CE%u6247%uFF0C%u8981%u5BF9Alexa%u8BF4%u201CTurn%20on%20the%20Fan%u201D%uFF0C%u8981%u5173%u5219%u8FD8%u662F%u8BF4%u201CTurn%20on%20the%20Fan%u201D%u3002%u662F%u4E0D%u662F%u6709%u70B9%u5947%u602A%u3002%0A%u5982%u679C%u662FHA-Bridge%uFF0C%u9488%u5BF9%u5F00%u5173%u72B6%u6001%uFF0C%u5C31%u53EF%u4EE5%u8BF4%u201CTurn%20on%20the%20Fan%u201D%u548C%u201CTurn%20off%20the%20Fan%u201D%u3002%u662F%u4E0D%u662F%u597D%u591A%u4E86%u3002%0AControlicz%u6574%u5408Domoticz%u7684%u597D%u5904%u5C31%u662F%uFF0CDomoticz%20Broadlink%20Python%20plugin%u652F%u6301%u76F4%u63A5%u4E00%u6B21%u6027%u4ECE%u6613%u63A7%u5BFC%u5165%u5DF2%u5B9A%u4E49%u8BBE%u5907%uFF0C%u53EF%u4EE5%u5145%u5206%u53D1%u6325%u6613%u63A7GUI%u7684%u7279%u70B9%uFF0C%u7701%u5F97%u4E00%u4E2A%u4E00%u4E2A%u8BBE%u5907%u81EA%u5DF1%u5B9A%u4E49%u4E86%u3002%0A%0A%23%23%23%20Broadlink%u539F%u751FSkill%20+%20IHC%0A%u5728%u6574%u5B8C%u4E0A%u9762%u90A3%u4E00%u5927%u5806%u4E4B%u540E%uFF0C%u6211%u7A81%u7136%u53D1%u73B0%u539F%u6765Alexa%u5DF2%u7ECF%u6709Broadlink%u7684%u539F%u751FSkill%u4E86%uFF0C%u4F46%u8981%u914D%u5408%u6700%u65B0%u7248%u7684Broadlink%20IHC%20app%u4F7F%u7528%u3002%u4E0D%u8FC7%u4EE4%u4EBA%u7CDF%u5FC3%u7684%u662F%u8FD9%u4E24%u4E2Aapp%u7684%u8BC4%u5206%u90FD%u4F4E%u7684%u53EF%u601C%u3002%0A-%20IHC%20playstore%202.2%u5206%0A-%20Alexa%20Broadlink%20Skill%202.3%u5206%0A%u4ECE%u5B98%u7F51%u7684example%u770B%u76EE%u524D%u53EA%u652F%u6301TV%u3002%u7ECF%u6211%u5B9E%u8DF5%u786E%u5B9E%u5982%u6B64%uFF0C%u6DFB%u52A0%u4E86%u7535%u89C6%u548C%u7A7A%u8C03%uFF0CAlexa%u5374%u53EA%u80FDdiscover%u5230%u7535%u89C6%uFF0C%u6CA1%u6709%u7A7A%u8C03%u3002%0A%0A%0A%23%23%u53C2%u8003%u94FE%u63A5%0A1.%20%5B5%20open%20source%20home%20automation%20tools%5D%28https%3A//opensource.com/life/16/3/5-open-source-home-automation-tools%29%0A2.%20%5BAlex%20-%20Domoticz%5D%28https%3A//www.domoticz.com/wiki/Alexa%29%0A3.%20%5BDomoticz%20Wiki%20Plugins/BroadlinkRM2%5D%28http%3A//www.domoticz.com/wiki/Plugins/BroadlinkRM2.html%23First_Time_Users%29%0A4.%20%5B%23%u539F%u521B%u65B0%u4EBA%23%20%u7FA4%u6656%u5B89%u88C5broadlink-http-rest%u4EE3%u66FFRMBridge_%u751F%u6D3B%u8BB0%u5F55_%u4EC0%u4E48%u503C%u5F97%u4E70%5D%28https%3A//post.smzdm.com/p/551024/%29

Edit

Overview

最近有一个项目想做chat bot,也就是在IM app上自动回答问题的机器人。要回答问题,就要对问题有所了解,于是最近看了一些NLP (Natural Language Process)方面的资料。没有过多的涉及基础理论以及模型方面的知识。因为是做项目,更多的是希望能从工程方面直接进行应用。本文主要涉及SyntaxNet和NLTK。前者是Google于2016年开源的NLP项目,其包含了基本模型以及基于TensorFlow的实现。而且其基于若干训练资料,已经有一个pre-trained English model。可以直接被加以利用。

SyntaxNet

安装

一些参考网页

安装完成后,运行一个demo程序如下:

echo 'Bob brought the pizza to Alice.' | syntaxnet/demo.sh

结果是一个树状结构

Input: Bob brought the pizza to Alice .
Parse:
brought VBD ROOT
+-- Bob NNP nsubj
+-- pizza NN dobj
| +-- the DT det
+-- to IN prep
| +-- Alice NNP pobj
+-- . . punct

SyntaxNet自带的pre-trained English parser叫Parsey McParseface。我们可以用这个parser来分析语句。根据How to Install and Use SyntaxNet and Parsey McParseface中所述,Parsey McParseface输出实为CoNLL table。这个table的格式在models/syntaxnet/syntaxnet/text_formats.cc,如下:

 50 // CoNLL document format reader for dependency annotated corpora.
51 // The expected format is described e.g. at http://ilk.uvt.nl/conll/#dataformat
52 //
53 // Data should adhere to the following rules:
54 // - Data files contain sentences separated by a blank line.
55 // - A sentence consists of one or tokens, each one starting on a new line.
56 // - A token consists of ten fields described in the table below.
57 // - Fields are separated by a single tab character.
58 // - All data files will contains these ten fields, although only the ID
59 // column is required to contain non-dummy (i.e. non-underscore) values.
60 // Data files should be UTF-8 encoded (Unicode).
61 //
62 // Fields:
63 // 1 ID: Token counter, starting at 1 for each new sentence and increasing
64 // by 1 for every new token.
65 // 2 FORM: Word form or punctuation symbol.
66 // 3 LEMMA: Lemma or stem.
67 // 4 CPOSTAG: Coarse-grained part-of-speech tag or category.
68 // 5 POSTAG: Fine-grained part-of-speech tag. Note that the same POS tag
69 // cannot appear with multiple coarse-grained POS tags.
70 // 6 FEATS: Unordered set of syntactic and/or morphological features.
71 // 7 HEAD: Head of the current token, which is either a value of ID or '0'.
72 // 8 DEPREL: Dependency relation to the HEAD.
73 // 9 PHEAD: Projective head of current token.
74 // 10 PDEPREL: Dependency relation to the PHEAD.

直接使用CoNLL table更易于被代码解析。如果需要CoNLL table输出,需要我们修改demo.sh。直接来个例子:

INFO:tensorflow:Processed 1 documents
1 What _ PRON WP _ 0 ROOT _ _
2 is _ VERB VBZ _ 1 cop _ _
3 a _ DET DT _ 5 det _ _
4 control _ NOUN NN _ 5 nn _ _
5 panel _ NOUN NN _ 1 nsubj _ _

这是对What is a control panel的输出。下图源于Inside Google SyntaxNet

CoNLL table中的所有tag缩写的含义在这里Universal Dependency Relations

针对CPOSTAG & POSTAG,可参考

NLTK

NLTK is a leading platform for building Python programs to work with human language data. It provides easy-to-use interfaces to over 50 corpora and lexical resources such as WordNet, along with a suite of text processing libraries for classification, tokenization, stemming, tagging, parsing, and semantic reasoning, wrappers for industrial-strength NLP libraries, and an active discussion forum.

我的想法是可以用NLTK来对SyntaxNet的输出做进一步Stemming/Lemmarization处理。

NLTK vs. spaCy

Stemming vs. Lemmatization

这两个词总是一起出现,作用很相像。区别是:

Lemmatisation is closely related to stemming. The difference is that a stemmer operates on a single word without knowledge of the context, and therefore cannot discriminate between words which have different meanings depending on part of speech. However, stemmers are typically easier to implement and run faster, and the reduced accuracy may not matter for some applications.

In computational linguistics, lemmatisation is the algorithmic process of determining the lemma for a given word. Since the process may involve complex tasks such as understanding context and determining the part of speech of a word in a sentence (requiring, for example, knowledge of the grammar of a language) it can be a hard task to implement a lemmatiser for a new language.

简而言之,就是Lemmarization是包含有上下文含义的,而Stemming只是对单个单词进行映射。

NLTK支持多种Stemmer,包括但不限于 Porter stemmer, Lancaster Stemmer, Snowball Stemmer。

>>> from nltk.stem import SnowballStemmer
>>> snowball_stemmer = SnowballStemmer(“english”)
>>> snowball_stemmer.stem(‘maximum’)
u’maximum’
>>> snowball_stemmer.stem(‘presumably’)
u’presum’
>>> snowball_stemmer.stem(‘multiply’)
u’multipli’

NLTK中的Lemarize:

>>> from nltk.stem import WordNetLemmatizer
>>> wordnet_lemmatizer = WordNetLemmatizer()
>>> wordnet_lemmatizer.lemmatize(‘dogs’)
u’dog’
>>> wordnet_lemmatizer.lemmatize(‘churches’)
u’church’
>>> wordnet_lemmatizer.lemmatize(‘is’, pos=’v’)
u’be’
>>> wordnet_lemmatizer.lemmatize(‘are’, pos=’v’)
u’be’
>>>
  • pos = Part Of Speech
%23NLP%u8BB0%u5F55%0A@%28myblog%29%5Bnlp%5D%0A%0A%5BTOC%5D%0A%0A%23%23Overview%0A%u6700%u8FD1%u6709%u4E00%u4E2A%u9879%u76EE%u60F3%u505Achat%20bot%uFF0C%u4E5F%u5C31%u662F%u5728IM%20app%u4E0A%u81EA%u52A8%u56DE%u7B54%u95EE%u9898%u7684%u673A%u5668%u4EBA%u3002%u8981%u56DE%u7B54%u95EE%u9898%uFF0C%u5C31%u8981%u5BF9%u95EE%u9898%u6709%u6240%u4E86%u89E3%uFF0C%u4E8E%u662F%u6700%u8FD1%u770B%u4E86%u4E00%u4E9BNLP%20%28Natural%20Language%20Process%29%u65B9%u9762%u7684%u8D44%u6599%u3002%u6CA1%u6709%u8FC7%u591A%u7684%u6D89%u53CA%u57FA%u7840%u7406%u8BBA%u4EE5%u53CA%u6A21%u578B%u65B9%u9762%u7684%u77E5%u8BC6%u3002%u56E0%u4E3A%u662F%u505A%u9879%u76EE%uFF0C%u66F4%u591A%u7684%u662F%u5E0C%u671B%u80FD%u4ECE%u5DE5%u7A0B%u65B9%u9762%u76F4%u63A5%u8FDB%u884C%u5E94%u7528%u3002%u672C%u6587%u4E3B%u8981%u6D89%u53CA%5BSyntaxNet%5D%28https%3A//github.com/tensorflow/models/tree/master/syntaxnet%29%u548CNLTK%u3002%u524D%u8005%u662FGoogle%u4E8E2016%u5E74%u5F00%u6E90%u7684NLP%u9879%u76EE%uFF0C%u5176%u5305%u542B%u4E86%u57FA%u672C%u6A21%u578B%u4EE5%u53CA%u57FA%u4E8ETensorFlow%u7684%u5B9E%u73B0%u3002%u800C%u4E14%u5176%u57FA%u4E8E%u82E5%u5E72%u8BAD%u7EC3%u8D44%u6599%uFF0C%u5DF2%u7ECF%u6709%u4E00%u4E2Apre-trained%20English%20model%u3002%u53EF%u4EE5%u76F4%u63A5%u88AB%u52A0%u4EE5%u5229%u7528%u3002%0A%0A%23%23%23SyntaxNet%0A%23%23%23%23%u5B89%u88C5%0A%u4E00%u4E9B%u53C2%u8003%u7F51%u9875%0A-%20%5BNLP%u521D%u7EA7%u9009%u624Bubuntu%20%u4E0B%u5B89%u88C5google%20SyntaxNet%5D%28%5D%28http%3A//blog.csdn.net/u012507864/article/details/51478060%29%29%0A-%20%5BHow%20to%20Install%20and%20Use%20SyntaxNet%20and%20Parsey%20McParseface%5D%28http%3A//www.whycouch.com/2016/07/how-to-install-and-use-syntaxnet-and.html%29%0A-%20%5BSyntaxNet%20Tutorial%5D%28https%3A//github.com/tensorflow/models/blob/master/syntaxnet/g3doc/syntaxnet-tutorial.md%29%0A-%20%5BSyntaxNet%3A%20Understanding%20the%20Parser%5D%28http%3A//jduelfer.github.io/syntaxnet%2C/tensorflow/2016/08/20/understanding-the-parser.html%29%0A%0A%u5B89%u88C5%u5B8C%u6210%u540E%uFF0C%u8FD0%u884C%u4E00%u4E2Ademo%u7A0B%u5E8F%u5982%u4E0B%uFF1A%0A%60%60%60%0Aecho%20%27Bob%20brought%20the%20pizza%20to%20Alice.%27%20%7C%20syntaxnet/demo.sh%0A%60%60%60%0A%u7ED3%u679C%u662F%u4E00%u4E2A%u6811%u72B6%u7ED3%u6784%0A%60%60%60%0AInput%3A%20Bob%20brought%20the%20pizza%20to%20Alice%20.%0AParse%3A%0Abrought%20VBD%20ROOT%0A%20+--%20Bob%20NNP%20nsubj%0A%20+--%20pizza%20NN%20dobj%0A%20%7C%20%20%20+--%20the%20DT%20det%0A%20+--%20to%20IN%20prep%0A%20%7C%20%20%20+--%20Alice%20NNP%20pobj%0A%20+--%20.%20.%20punct%0A%60%60%60%0ASyntaxNet%u81EA%u5E26%u7684pre-trained%20English%20parser%u53EBParsey%20McParseface%u3002%u6211%u4EEC%u53EF%u4EE5%u7528%u8FD9%u4E2Aparser%u6765%u5206%u6790%u8BED%u53E5%u3002%u6839%u636E%5BHow%20to%20Install%20and%20Use%20SyntaxNet%20and%20Parsey%20McParseface%5D%28http%3A//www.whycouch.com/2016/07/how-to-install-and-use-syntaxnet-and.html%29%u4E2D%u6240%u8FF0%uFF0CParsey%20McParseface%u8F93%u51FA%u5B9E%u4E3ACoNLL%20table%u3002%u8FD9%u4E2Atable%u7684%u683C%u5F0F%u5728%60models/syntaxnet/syntaxnet/text_formats.cc%60%uFF0C%u5982%u4E0B%uFF1A%0A%60%60%60%0A%2050%20//%20CoNLL%20document%20format%20reader%20for%20dependency%20annotated%20corpora.%0A%2051%20//%20The%20expected%20format%20is%20described%20e.g.%20at%20http%3A//ilk.uvt.nl/conll/%23dataformat%0A%2052%20//%0A%2053%20//%20Data%20should%20adhere%20to%20the%20following%20rules%3A%0A%2054%20//%20%20%20-%20Data%20files%20contain%20sentences%20separated%20by%20a%20blank%20line.%0A%2055%20//%20%20%20-%20A%20sentence%20consists%20of%20one%20or%20tokens%2C%20each%20one%20starting%20on%20a%20new%20line.%0A%2056%20//%20%20%20-%20A%20token%20consists%20of%20ten%20fields%20described%20in%20the%20table%20below.%0A%2057%20//%20%20%20-%20Fields%20are%20separated%20by%20a%20single%20tab%20character.%0A%2058%20//%20%20%20-%20All%20data%20files%20will%20contains%20these%20ten%20fields%2C%20although%20only%20the%20ID%0A%2059%20//%20%20%20%20%20column%20is%20required%20to%20contain%20non-dummy%20%28i.e.%20non-underscore%29%20values.%0A%2060%20//%20Data%20files%20should%20be%20UTF-8%20encoded%20%28Unicode%29.%0A%2061%20//%0A%2062%20//%20Fields%3A%0A%2063%20//%201%20%20ID%3A%20%20%20%20%20%20Token%20counter%2C%20starting%20at%201%20for%20each%20new%20sentence%20and%20increasing%0A%2064%20//%20%20%20%20%20%20%20%20%20%20%20%20%20by%201%20for%20every%20new%20token.%0A%2065%20//%202%20%20FORM%3A%20%20%20%20Word%20form%20or%20punctuation%20symbol.%0A%2066%20//%203%20%20LEMMA%3A%20%20%20Lemma%20or%20stem.%0A%2067%20//%204%20%20CPOSTAG%3A%20Coarse-grained%20part-of-speech%20tag%20or%20category.%0A%2068%20//%205%20%20POSTAG%3A%20%20Fine-grained%20part-of-speech%20tag.%20Note%20that%20the%20same%20POS%20tag%0A%2069%20//%20%20%20%20%20%20%20%20%20%20%20%20%20cannot%20appear%20with%20multiple%20coarse-grained%20POS%20tags.%0A%2070%20//%206%20%20FEATS%3A%20%20%20Unordered%20set%20of%20syntactic%20and/or%20morphological%20features.%0A%2071%20//%207%20%20HEAD%3A%20%20%20%20Head%20of%20the%20current%20token%2C%20which%20is%20either%20a%20value%20of%20ID%20or%20%270%27.%0A%2072%20//%208%20%20DEPREL%3A%20%20Dependency%20relation%20to%20the%20HEAD.%0A%2073%20//%209%20%20PHEAD%3A%20%20%20Projective%20head%20of%20current%20token.%0A%2074%20//%2010%20PDEPREL%3A%20Dependency%20relation%20to%20the%20PHEAD.%0A%60%60%60%0A%u76F4%u63A5%u4F7F%u7528CoNLL%20table%u66F4%u6613%u4E8E%u88AB%u4EE3%u7801%u89E3%u6790%u3002%u5982%u679C%u9700%u8981CoNLL%20table%u8F93%u51FA%uFF0C%u9700%u8981%u6211%u4EEC%u4FEE%u6539demo.sh%u3002%u76F4%u63A5%u6765%u4E2A%u4F8B%u5B50%uFF1A%0A%60%60%60%0AINFO%3Atensorflow%3AProcessed%201%20documents%0A1%20%20%20%20%20%20%20What%20%20%20%20_%20%20%20%20%20%20%20PRON%20%20%20%20WP%20%20%20%20%20%20_%20%20%20%20%20%20%200%20%20%20%20%20%20%20ROOT%20%20%20%20_%20%20%20%20%20%20%20_%0A2%20%20%20%20%20%20%20is%20%20%20%20%20%20_%20%20%20%20%20%20%20VERB%20%20%20%20VBZ%20%20%20%20%20_%20%20%20%20%20%20%201%20%20%20%20%20%20%20cop%20%20%20%20%20_%20%20%20%20%20%20%20_%0A3%20%20%20%20%20%20%20a%20%20%20%20%20%20%20_%20%20%20%20%20%20%20DET%20%20%20%20%20DT%20%20%20%20%20%20_%20%20%20%20%20%20%205%20%20%20%20%20%20%20det%20%20%20%20%20_%20%20%20%20%20%20%20_%0A4%20%20%20%20%20%20%20control%20_%20%20%20%20%20%20%20NOUN%20%20%20%20NN%20%20%20%20%20%20_%20%20%20%20%20%20%205%20%20%20%20%20%20%20nn%20%20%20%20%20%20_%20%20%20%20%20%20%20_%0A5%20%20%20%20%20%20%20panel%20%20%20_%20%20%20%20%20%20%20NOUN%20%20%20%20NN%20%20%20%20%20%20_%20%20%20%20%20%20%201%20%20%20%20%20%20%20nsubj%20%20%20_%20%20%20%20%20%20%20_%0A%60%60%60%0A%u8FD9%u662F%u5BF9What%20is%20a%20control%20panel%u7684%u8F93%u51FA%u3002%u4E0B%u56FE%u6E90%u4E8E%5BInside%20Google%20SyntaxNet%5D%28http%3A//andrewmatteson.name/index.php/2017/02/04/inside-syntaxnet/%29%0A%21%5BAlt%20text%5D%28./1499230365640.png%29%0A%u53EF%u89C1%u7B2C3%2C6%2C9%2C10%u5B57%u6BB5%uFF0CSytanxNet%u5E76%u6CA1%u6709%u8F93%u51FA%0A%0ACoNLL%20table%u4E2D%u7684%u6240%u6709tag%u7F29%u5199%u7684%u542B%u4E49%u5728%u8FD9%u91CC%5BUniversal%20Dependency%20Relations%5D%28http%3A//universaldependencies.org/u/dep/%29%0A%0A%u9488%u5BF9CPOSTAG%20%26%20POSTAG%uFF0C%u53EF%u53C2%u8003%0A-%20%5BAlphabetical%20list%20of%20part-of-speech%20tags%20used%20in%20the%20Penn%20Treebank%20Project%5D%28https%3A//www.ling.upenn.edu/courses/Fall_2003/ling001/penn_treebank_pos.html%29%0A-%20%5BWh-%20words%5D%28http%3A//www.ling.upenn.edu/histcorpora/annotation/pos-wh.htm%29%0A%0A%0A%0A%23%23%23NLTK%0A%3E**NLTK**%20is%20a%20leading%20platform%20for%20building%20**Python**%20programs%20to%20work%20with%20human%20language%20data.%20It%20provides%20easy-to-use%20interfaces%20to%20over%2050%20corpora%20and%20lexical%20resources%20such%20as%20WordNet%2C%20along%20with%20a%20suite%20of%20text%20processing%20libraries%20for%20classification%2C%20tokenization%2C%20stemming%2C%20tagging%2C%20parsing%2C%20and%20semantic%20reasoning%2C%20wrappers%20for%20industrial-strength%20NLP%20libraries%2C%20and%20an%20active%20discussion%20forum.%0A%0A%u6211%u7684%u60F3%u6CD5%u662F%u53EF%u4EE5%u7528NLTK%u6765%u5BF9SyntaxNet%u7684%u8F93%u51FA%u505A%u8FDB%u4E00%u6B65Stemming/Lemmarization%u5904%u7406%u3002%0A%0A%23%23%23%23%5BNLTK%20vs.%20spaCy%5D%28http%3A//blog.thedataincubator.com/2016/04/nltk-vs-spacy-natural-language-processing-in-python/%29%0A%0A%23%23%23%23Stemming%20vs.%20Lemmatization%0A%u8FD9%u4E24%u4E2A%u8BCD%u603B%u662F%u4E00%u8D77%u51FA%u73B0%uFF0C%u4F5C%u7528%u5F88%u76F8%u50CF%u3002%u533A%u522B%u662F%uFF1A%0A%3ELemmatisation%20is%20closely%20related%20to%20stemming.%20The%20difference%20is%20that%20**a%20stemmer%20operates%20on%20a%20single%20word%20without%20knowledge%20of%20the%20context**%2C%20and%20therefore%20cannot%20discriminate%20between%20words%20which%20have%20different%20meanings%20depending%20on%20part%20of%20speech.%20However%2C%20stemmers%20are%20typically%20easier%20to%20implement%20and%20run%20faster%2C%20and%20the%20reduced%20accuracy%20may%20not%20matter%20for%20some%20applications.%0A%0A%3EIn%20computational%20linguistics%2C%20lemmatisation%20is%20the%20algorithmic%20process%20of%20determining%20the%20lemma%20for%20a%20given%20word.%20**Since%20the%20process%20may%20involve%20complex%20tasks%20such%20as%20understanding%20context%20and%20determining%20the%20part%20of%20speech%20of%20a%20word%20in%20a%20sentence**%20%28requiring%2C%20for%20example%2C%20knowledge%20of%20the%20grammar%20of%20a%20language%29%20it%20can%20be%20a%20hard%20task%20to%20implement%20a%20lemmatiser%20for%20a%20new%20language.%0A%0A%u7B80%u800C%u8A00%u4E4B%uFF0C%u5C31%u662FLemmarization%u662F%u5305%u542B%u6709%u4E0A%u4E0B%u6587%u542B%u4E49%u7684%uFF0C%u800CStemming%u53EA%u662F%u5BF9%u5355%u4E2A%u5355%u8BCD%u8FDB%u884C%u6620%u5C04%u3002%0A%0ANLTK%u652F%u6301%u591A%u79CDStemmer%uFF0C%u5305%u62EC%u4F46%u4E0D%u9650%u4E8E%20Porter%20stemmer%2C%20Lancaster%20Stemmer%2C%20Snowball%20Stemmer%u3002%0A%60%60%60python%0A%3E%3E%3E%20from%20nltk.stem%20import%20SnowballStemmer%0A%3E%3E%3E%20snowball_stemmer%20%3D%20SnowballStemmer%28%u201Cenglish%u201D%29%0A%3E%3E%3E%20snowball_stemmer.stem%28%u2018maximum%u2019%29%0Au%u2019maximum%u2019%0A%3E%3E%3E%20snowball_stemmer.stem%28%u2018presumably%u2019%29%0Au%u2019presum%u2019%0A%3E%3E%3E%20snowball_stemmer.stem%28%u2018multiply%u2019%29%0Au%u2019multipli%u2019%0A%60%60%60%0ANLTK%u4E2D%u7684Lemarize%3A%20%0A%60%60%60python%0A%3E%3E%3E%20from%20nltk.stem%20import%20WordNetLemmatizer%0A%3E%3E%3E%20wordnet_lemmatizer%20%3D%20WordNetLemmatizer%28%29%0A%3E%3E%3E%20wordnet_lemmatizer.lemmatize%28%u2018dogs%u2019%29%0Au%u2019dog%u2019%0A%3E%3E%3E%20wordnet_lemmatizer.lemmatize%28%u2018churches%u2019%29%0Au%u2019church%u2019%0A%3E%3E%3E%20wordnet_lemmatizer.lemmatize%28%u2018is%u2019%2C%20pos%3D%u2019v%u2019%29%0Au%u2019be%u2019%0A%3E%3E%3E%20wordnet_lemmatizer.lemmatize%28%u2018are%u2019%2C%20pos%3D%u2019v%u2019%29%0Au%u2019be%u2019%0A%3E%3E%3E%0A%60%60%60%0A*%20pos%20%3D%20Part%20Of%20Speech

Edit

写博客的想法由来已久,更多的分享才有更多的获得。因为平时GitHub用的很多,所以选择在GitHub上搭建博客。一开始看到的是Jekyll,后来看到用Hexo的帖子,觉得Hexo也不错,于是就选定从Hexo开始了。
因为平时多用Evernote记录各种读书笔记,折腾历程,所以希望能找到一个直接用Evernote中笔记作为blog post的工具。最后在npm上发现了everblog,正好符合自己的需求,但当然免不了各种踩坑。好在最后顺利解决。本文会有所记录。

在GitHub上搭建Hexo博客

在google上一搜一大摞。贴出我参考的链接手把手教你使用Hexo + Github Pages搭建个人独立博客

简单来说就是:

  1. 在GitHub上注册一个如此命名的repo: <username>.github.io, e.g. zhougy0717.github.io
  2. 用hexo工具来初始化这个repo
  3. blog所有的配置信息都在_config.yml文件中,包括deploy的信息
deploy:
type: git
repo: https://github.com/zhougy0717/zhougy0717.github.io.git
branch: master
  1. 所有的post在source/_posts

Hexo的逻辑是在_posts下用Markdown写博客,然后调用hexo g对Markdown post进行渲染生成html,最后调用hexo s将生成的网页版post发布到GitHub上去,从而可以用.github.io的域名访问到该博客。

Everblog

项目地址: everblog
使用方法:

  1. npm install everblog -g
  2. 在主目录下添加.everblogrc,包含字段:
    • token
    • noteStoreUrl
    • notebook
  3. 在blog根目录下添加index.js
module.exports = require('everblog-adaptor-hexo-html')
  1. 在blog根目录下运行everblog build
  2. hexo s测试,hexo d部署

踩坑记录

EDAMSystemException: authenticationToken

各种翻看Evernote API和Evernote Nodejs SDK, 最后还是用代码单步得知,是serviceHost的问题。servicceHost默认采用”www.evernote.com”。而这个值是可以传给Evernote.Client的。而由于everblog将.everblogrc中的值直接传给Evernote.Client,所以我们可以在.everblogrc中添加serviceHost值。这样就可以成功build了。

HTML based Hexo blog

由于Hexo的原生逻辑是用Markdown写blog,而前文提到,本次搭建博客希望基于Evernote。这样每次用马克飞象写完笔记后,直接发布就可以了。但Windows下类MWeb的工具都得付钱。而且这些工具包括MWeb,都需自行维护笔记原始资源,然后将渲染后的Markdown发布到各Web Service。所以,还是希望能保留Evernote + 马克飞象工作方式。
everblog自带的adaptor是everblog-adaptor-hexo,其工作方式是导出笔记中的纯文本,然后生成Markdown文件发布。其基于everblog作者的另外一个project——enml2text。everblog作者还有一个project——enml2html。
于是最后的解决方式是,对笔记的content,调用enml2html生成html文件。但有一个问题,生成的html的文件无法显示图片。
这是因为enml2html生成图片路径的方式过期了,根据Evernote developer网页的方法,进行了更新并提交了pull request。

这时仍然有另一个问题,就是印象笔记的图片是不允许外链的。也就是在你没登录印象笔记网页用户的时候,博客里面的图片统统不能显示。于是乎,就只能将图片下载到本地,并在img src字段进行标注,来实现显示图片了。

本地图片显示

承接上文。幸运的是,网上是有这种需求的。于是搜到这篇网页:Hexo框架下给博客插入本地图片。于是基于这个网页中提到的方法,修改everblog-adaptor-hexo实现下载网页并能显示图片。但inline图片(MathJax渲染的公式图片)尺寸变得很大,查了一下evernote客户端导出的html,这些图片都得到了一定程度的缩放。不知道其缩放逻辑是什么。不想那么多,于是我对所有__SVG__开头的图片都进行了47.5%的缩小。

引用文字中对齐问题

Hexo theme的问题。试了几个them,最终用的是freemind没有这个问题。

%23%20%u5728GitHub%u4E0A%u7528Evernote+Hexo%u642D%u5EFA%u4E2A%u4EBA%u9759%u6001%u535A%u5BA2%0A@%28myblog%29%5Bhexo%2C%20GitHub%5D%0A%0A%5BTOC%5D%0A%0A%u5199%u535A%u5BA2%u7684%u60F3%u6CD5%u7531%u6765%u5DF2%u4E45%uFF0C%u66F4%u591A%u7684%u5206%u4EAB%u624D%u6709%u66F4%u591A%u7684%u83B7%u5F97%u3002%u56E0%u4E3A%u5E73%u65F6GitHub%u7528%u7684%u5F88%u591A%uFF0C%u6240%u4EE5%u9009%u62E9%u5728GitHub%u4E0A%u642D%u5EFA%u535A%u5BA2%u3002%u4E00%u5F00%u59CB%u770B%u5230%u7684%u662FJekyll%uFF0C%u540E%u6765%u770B%u5230%u7528Hexo%u7684%u5E16%u5B50%uFF0C%u89C9%u5F97Hexo%u4E5F%u4E0D%u9519%uFF0C%u4E8E%u662F%u5C31%u9009%u5B9A%u4ECEHexo%u5F00%u59CB%u4E86%u3002%0A%u56E0%u4E3A%u5E73%u65F6%u591A%u7528Evernote%u8BB0%u5F55%u5404%u79CD%u8BFB%u4E66%u7B14%u8BB0%uFF0C%u6298%u817E%u5386%u7A0B%uFF0C%u6240%u4EE5%u5E0C%u671B%u80FD%u627E%u5230%u4E00%u4E2A%u76F4%u63A5%u7528Evernote%u4E2D%u7B14%u8BB0%u4F5C%u4E3Ablog%20post%u7684%u5DE5%u5177%u3002%u6700%u540E%u5728npm%u4E0A%u53D1%u73B0%u4E86everblog%uFF0C%u6B63%u597D%u7B26%u5408%u81EA%u5DF1%u7684%u9700%u6C42%uFF0C%u4F46%u5F53%u7136%u514D%u4E0D%u4E86%u5404%u79CD%u8E29%u5751%u3002%u597D%u5728%u6700%u540E%u987A%u5229%u89E3%u51B3%u3002%u672C%u6587%u4F1A%u6709%u6240%u8BB0%u5F55%u3002%0A%0A%23%23%20%u5728GitHub%u4E0A%u642D%u5EFAHexo%u535A%u5BA2%0A%u5728google%u4E0A%u4E00%u641C%u4E00%u5927%u645E%u3002%u8D34%u51FA%u6211%u53C2%u8003%u7684%u94FE%u63A5%5B%u624B%u628A%u624B%u6559%u4F60%u4F7F%u7528Hexo%20+%20Github%20Pages%u642D%u5EFA%u4E2A%u4EBA%u72EC%u7ACB%u535A%u5BA2%5D%28https%3A//linghucong.js.org/2016/04/15/2016-04-15-hexo-github-pages-blog/%29%0A%0A%u7B80%u5355%u6765%u8BF4%u5C31%u662F%uFF1A%0A1.%20%u5728GitHub%u4E0A%u6CE8%u518C%u4E00%u4E2A%u5982%u6B64%u547D%u540D%u7684repo%3A%20%26lt%3Busername%26gt%3B.github.io%2C%20e.g.%20zhougy0717.github.io%0A2.%20%u7528hexo%u5DE5%u5177%u6765%u521D%u59CB%u5316%u8FD9%u4E2Arepo%0A3.%20blog%u6240%u6709%u7684%u914D%u7F6E%u4FE1%u606F%u90FD%u5728_config.yml%u6587%u4EF6%u4E2D%uFF0C%u5305%u62ECdeploy%u7684%u4FE1%u606F%0A%60%60%60yaml%0Adeploy%3A%0A%20%20type%3A%20git%0A%20%20repo%3A%20https%3A//github.com/zhougy0717/zhougy0717.github.io.git%0A%20%20branch%3A%20master%0A%60%60%60%0A4.%20%u6240%u6709%u7684post%u5728%60source/_posts%60%u4E0B%0A%0AHexo%u7684%u903B%u8F91%u662F%u5728_posts%u4E0B%u7528Markdown%u5199%u535A%u5BA2%uFF0C%u7136%u540E%u8C03%u7528%60hexo%20g%60%u5BF9Markdown%20post%u8FDB%u884C%u6E32%u67D3%u751F%u6210html%uFF0C%u6700%u540E%u8C03%u7528%60hexo%20s%60%u5C06%u751F%u6210%u7684%u7F51%u9875%u7248post%u53D1%u5E03%u5230GitHub%u4E0A%u53BB%uFF0C%u4ECE%u800C%u53EF%u4EE5%u7528.github.io%u7684%u57DF%u540D%u8BBF%u95EE%u5230%u8BE5%u535A%u5BA2%u3002%0A%0A%23%23Everblog%0A%u9879%u76EE%u5730%u5740%3A%20%5Beverblog%5D%28https%3A//github.com/everblogjs/everblog%29%0A%u4F7F%u7528%u65B9%u6CD5%uFF1A%0A1.%20npm%20install%20everblog%20-g%0A2.%20%u5728%u4E3B%u76EE%u5F55%u4E0B%u6DFB%u52A0.everblogrc%uFF0C%u5305%u542B%u5B57%u6BB5%uFF1A%0A%09-%20token%0A%09-%20noteStoreUrl%0A%09-%20notebook%0A3.%20%u5728blog%u6839%u76EE%u5F55%u4E0B%u6DFB%u52A0index.js%0A%60%60%60%20javascript%0Amodule.exports%20%3D%20require%28%27everblog-adaptor-hexo-html%27%29%0A%60%60%60%0A3.%20%u5728blog%u6839%u76EE%u5F55%u4E0B%u8FD0%u884C%60everblog%20build%60%0A4.%20%60hexo%20s%60%u6D4B%u8BD5%uFF0C%60hexo%20d%60%u90E8%u7F72%0A%0A%23%23%23%20%u8E29%u5751%u8BB0%u5F55%0A%23%23%23%23EDAMSystemException%3A%20authenticationToken%0A%u5404%u79CD%u7FFB%u770BEvernote%20API%u548CEvernote%20Nodejs%20SDK%uFF0C%20%u6700%u540E%u8FD8%u662F%u7528%u4EE3%u7801%u5355%u6B65%u5F97%u77E5%uFF0C%u662FserviceHost%u7684%u95EE%u9898%u3002servicceHost%u9ED8%u8BA4%u91C7%u7528%22www.evernote.com%22%u3002%u800C%u8FD9%u4E2A%u503C%u662F%u53EF%u4EE5%u4F20%u7ED9Evernote.Client%u7684%u3002%u800C%u7531%u4E8Eeverblog%u5C06.everblogrc%u4E2D%u7684%u503C%u76F4%u63A5%u4F20%u7ED9Evernote.Client%uFF0C%u6240%u4EE5%u6211%u4EEC%u53EF%u4EE5%u5728.everblogrc%u4E2D%u6DFB%u52A0serviceHost%u503C%u3002%u8FD9%u6837%u5C31%u53EF%u4EE5%u6210%u529Fbuild%u4E86%u3002%0A%0A%23%23%23%23HTML%20based%20Hexo%20blog%0A%u7531%u4E8EHexo%u7684%u539F%u751F%u903B%u8F91%u662F%u7528Markdown%u5199blog%uFF0C%u800C%u524D%u6587%u63D0%u5230%uFF0C%u672C%u6B21%u642D%u5EFA%u535A%u5BA2%u5E0C%u671B%u57FA%u4E8EEvernote%u3002%u8FD9%u6837%u6BCF%u6B21%u7528%u9A6C%u514B%u98DE%u8C61%u5199%u5B8C%u7B14%u8BB0%u540E%uFF0C%u76F4%u63A5%u53D1%u5E03%u5C31%u53EF%u4EE5%u4E86%u3002%u4F46Windows%u4E0B%u7C7BMWeb%u7684%u5DE5%u5177%u90FD%u5F97%u4ED8%u94B1%u3002%u800C%u4E14%u8FD9%u4E9B%u5DE5%u5177%u5305%u62ECMWeb%uFF0C%u90FD%u9700%u81EA%u884C%u7EF4%u62A4%u7B14%u8BB0%u539F%u59CB%u8D44%u6E90%uFF0C%u7136%u540E%u5C06%u6E32%u67D3%u540E%u7684Markdown%u53D1%u5E03%u5230%u5404Web%20Service%u3002%u6240%u4EE5%uFF0C%u8FD8%u662F%u5E0C%u671B%u80FD%u4FDD%u7559Evernote%20+%20%u9A6C%u514B%u98DE%u8C61%u5DE5%u4F5C%u65B9%u5F0F%u3002%0Aeverblog%u81EA%u5E26%u7684adaptor%u662Feverblog-adaptor-hexo%uFF0C%u5176%u5DE5%u4F5C%u65B9%u5F0F%u662F%u5BFC%u51FA%u7B14%u8BB0%u4E2D%u7684%u7EAF%u6587%u672C%uFF0C%u7136%u540E%u751F%u6210Markdown%u6587%u4EF6%u53D1%u5E03%u3002%u5176%u57FA%u4E8Eeverblog%u4F5C%u8005%u7684%u53E6%u5916%u4E00%u4E2Aproject%u2014%u2014enml2text%u3002everblog%u4F5C%u8005%u8FD8%u6709%u4E00%u4E2Aproject%u2014%u2014enml2html%u3002%0A%u4E8E%u662F%u6700%u540E%u7684%u89E3%u51B3%u65B9%u5F0F%u662F%uFF0C%u5BF9%u7B14%u8BB0%u7684content%uFF0C%u8C03%u7528enml2html%u751F%u6210html%u6587%u4EF6%u3002%u4F46%u6709%u4E00%u4E2A%u95EE%u9898%uFF0C%u751F%u6210%u7684html%u7684%u6587%u4EF6%u65E0%u6CD5%u663E%u793A%u56FE%u7247%u3002%0A%u8FD9%u662F%u56E0%u4E3Aenml2html%u751F%u6210%u56FE%u7247%u8DEF%u5F84%u7684%u65B9%u5F0F%u8FC7%u671F%u4E86%uFF0C%u6839%u636EEvernote%20developer%u7F51%u9875%u7684%u65B9%u6CD5%uFF0C%u8FDB%u884C%u4E86%u66F4%u65B0%u5E76%u63D0%u4EA4%u4E86pull%20request%u3002%0A%0A%u8FD9%u65F6%u4ECD%u7136%u6709%u53E6%u4E00%u4E2A%u95EE%u9898%uFF0C%u5C31%u662F%u5370%u8C61%u7B14%u8BB0%u7684%u56FE%u7247%u662F%u4E0D%u5141%u8BB8%u5916%u94FE%u7684%u3002%u4E5F%u5C31%u662F%u5728%u4F60%u6CA1%u767B%u5F55%u5370%u8C61%u7B14%u8BB0%u7F51%u9875%u7528%u6237%u7684%u65F6%u5019%uFF0C%u535A%u5BA2%u91CC%u9762%u7684%u56FE%u7247%u7EDF%u7EDF%u4E0D%u80FD%u663E%u793A%u3002%u4E8E%u662F%u4E4E%uFF0C%u5C31%u53EA%u80FD%u5C06%u56FE%u7247%u4E0B%u8F7D%u5230%u672C%u5730%uFF0C%u5E76%u5728img%20src%u5B57%u6BB5%u8FDB%u884C%u6807%u6CE8%uFF0C%u6765%u5B9E%u73B0%u663E%u793A%u56FE%u7247%u4E86%u3002%0A%0A%23%23%23%23%u672C%u5730%u56FE%u7247%u663E%u793A%0A%u627F%u63A5%u4E0A%u6587%u3002%u5E78%u8FD0%u7684%u662F%uFF0C%u7F51%u4E0A%u662F%u6709%u8FD9%u79CD%u9700%u6C42%u7684%u3002%u4E8E%u662F%u641C%u5230%u8FD9%u7BC7%u7F51%u9875%uFF1A%5BHexo%u6846%u67B6%u4E0B%u7ED9%u535A%u5BA2%u63D2%u5165%u672C%u5730%u56FE%u7247%5D%28https%3A//app.yinxiang.com/shard/s10/nl/161681/bee73bb9-bb81-45e9-b480-5260a70d3034%29%u3002%u4E8E%u662F%u57FA%u4E8E%u8FD9%u4E2A%u7F51%u9875%u4E2D%u63D0%u5230%u7684%u65B9%u6CD5%uFF0C%u4FEE%u6539everblog-adaptor-hexo%u5B9E%u73B0%u4E0B%u8F7D%u7F51%u9875%u5E76%u80FD%u663E%u793A%u56FE%u7247%u3002%u4F46inline%u56FE%u7247%uFF08MathJax%u6E32%u67D3%u7684%u516C%u5F0F%u56FE%u7247%uFF09%u5C3A%u5BF8%u53D8%u5F97%u5F88%u5927%uFF0C%u67E5%u4E86%u4E00%u4E0Bevernote%u5BA2%u6237%u7AEF%u5BFC%u51FA%u7684html%uFF0C%u8FD9%u4E9B%u56FE%u7247%u90FD%u5F97%u5230%u4E86%u4E00%u5B9A%u7A0B%u5EA6%u7684%u7F29%u653E%u3002%u4E0D%u77E5%u9053%u5176%u7F29%u653E%u903B%u8F91%u662F%u4EC0%u4E48%u3002%u4E0D%u60F3%u90A3%u4E48%u591A%uFF0C%u4E8E%u662F%u6211%u5BF9%u6240%u6709%60__SVG__%60%u5F00%u5934%u7684%u56FE%u7247%u90FD%u8FDB%u884C%u4E8647.5%25%u7684%u7F29%u5C0F%u3002%0A%0A%23%23%23%23%u5F15%u7528%u6587%u5B57%u4E2D%u5BF9%u9F50%u95EE%u9898%0AHexo%20theme%u7684%u95EE%u9898%u3002%u8BD5%u4E86%u51E0%u4E2Athem%uFF0C%u6700%u7EC8%u7528%u7684%u662Ffreemind%u6CA1%u6709%u8FD9%u4E2A%u95EE%u9898%u3002

Edit

Stochastic gradient descent

回忆线性回归的cost function:

m是sample数量,当m很大的时候,例如1,000,000,我们需要将所有的sample全部投入运算,这是很惊人的运算量。

Stochastic的意思就是每次只用一个sample,并不断进行迭代,从而达到和普通的gradient descent相同的效果。

  1. Randomly shuffle dataset
  2. Repeat
    • for i = 1, …, m
      • for j = 0, …, n

Mini-batch gradient descent

改进版的stochastic gradient descent

Batch gradient descent: Use all m examples in each iteration
Stochastic gradient descent: Use1 example in each iteration
Mini-batch gradient descent: Use b examples in each iteration

Say b = 10, m = 1000
Repeat{

  • for i = 1, 11, 21, 31,…,991{
    • for j = 0,…, n
    • }
  • }

在stochastic的图像中,我们可以看到,这种算法和batch算法比较,更多迂回婉转,难于收敛。所以在取的时候要格外小心。一般采用动态调整的办法设置

Learning rate is typically held constant. Can slowly decrease over timee if we want to converge.

Online learning

这其实是stochastic算法的一个应用。因为在每一次的迭代中,不需要所有的sample都参与,所以算法可以一边手机数据,一边进行迭代优化。

Map-reduce and data parallelism

%23Machine%20Learning%20%285%29%20-%20Large%20Scale%20Machine%20Learning%0A@%28myblog%29%5B%u6DF1%u5EA6%u5B66%u4E60%5D%0A%0A%0A%23%23Stochastic%20gradient%20descent%0A%0A%u56DE%u5FC6%u7EBF%u6027%u56DE%u5F52%u7684cost%20function%uFF1A%0A%24%24J%28%5Ctheta%29%20%3D%20%5Cdfrac%20%7B1%7D%7B2m%7D%5Csum_%7Bi%3D1%7D%5Em%28h_%5Ctheta%28x%5E%7B%28i%29%7D%29-y%5E%7B%28i%29%7D%29%5E2%24%24%0Am%u662Fsample%u6570%u91CF%uFF0C%u5F53m%u5F88%u5927%u7684%u65F6%u5019%uFF0C%u4F8B%u59821%2C000%2C000%uFF0C%u6211%u4EEC%u9700%u8981%u5C06%u6240%u6709%u7684sample%u5168%u90E8%u6295%u5165%u8FD0%u7B97%uFF0C%u8FD9%u662F%u5F88%u60CA%u4EBA%u7684%u8FD0%u7B97%u91CF%u3002%0A%0AStochastic%u7684%u610F%u601D%u5C31%u662F%u6BCF%u6B21%u53EA%u7528%u4E00%u4E2Asample%uFF0C%u5E76%u4E0D%u65AD%u8FDB%u884C%u8FED%u4EE3%uFF0C%u4ECE%u800C%u8FBE%u5230%u548C%u666E%u901A%u7684gradient%20descent%u76F8%u540C%u7684%u6548%u679C%u3002%0A%0A1.%20%24cost%28%5Ctheta%2C%20%28x%5E%7B%28i%29%7D%29%20-%20y%5E%7B%28i%29%7D%29%5E2%20%3D%20%5Cfrac%7B1%7D%7B2%7D%28h_%7B%5Ctheta%7D%28x%5E%7B%28i%29%7D%29%20-%20y%5E%7B%28i%29%7D%29%5E2%24%0A2.%20%24J_%7Btrain%7D%28%5Ctheta%29%20%3D%20%5Cfrac%20%7B1%7D%7Bm%7D%5Csum_%7Bi%3D1%7D%5Emcost%28%5Ctheta%2C%20%28x%5E%7B%28i%29%7D%2C%20y%5E%7B%28i%29%7D%29%29%24%0A3.%20Randomly%20shuffle%20dataset%0A4.%20Repeat%20%0A%09-%20for%20i%20%3D%201%2C%20...%2C%20m%0A%09%09%20*%20for%20j%20%3D%200%2C%20...%2C%20n%0A%09%09%09%24%5Ctheta_j%20%3A%3D%20%5Ctheta_j%20-%20%5Calpha%28h_%7B%5Ctheta%7D%28x%5E%7B%28i%29%7D%29-y%5E%7B%28i%29%7D%29x_j%5E%7B%28i%29%7D%24%0A%09%09%09%0A%21%5BAlt%20text%5D%28./1498184027550.png%29%0A%0A%23%23Mini-batch%20gradient%20descent%0A%u6539%u8FDB%u7248%u7684stochastic%20gradient%20descent%0A%3EBatch%20gradient%20descent%3A%20Use%20all%20m%20examples%20in%20each%20iteration%0AStochastic%20gradient%20descent%3A%20Use1%20example%20in%20each%20iteration%0AMini-batch%20gradient%20descent%3A%20Use%20b%20examples%20in%20each%20iteration%20%0A%0ASay%20b%20%3D%2010%2C%20m%20%3D%201000%0ARepeat%7B%0A-%20for%20i%20%3D%201%2C%2011%2C%2021%2C%2031%2C...%2C991%7B%0A%09*%20for%20j%20%3D%200%2C...%2C%20n%0A%09%09*%20%24%5Ctheta_j%20%3A%3D%20%5Ctheta_j%20-%20%5Calpha%20%5Cfrac%7B1%7D%7B10%7D%5Csum_%7Bk%3Di%7D%5E%7Bi+9%7D%28h_%7B%5Ctheta%7D%28x%5E%7B%28k%29%7D%29%20-%20y%5E%7B%28k%29%7D%29x_j%5E%7B%28k%29%7D%24%0A%09*%20%7D%0A-%20%7D%0A%0A%u5728stochastic%u7684%u56FE%u50CF%u4E2D%uFF0C%u6211%u4EEC%u53EF%u4EE5%u770B%u5230%uFF0C%u8FD9%u79CD%u7B97%u6CD5%u548Cbatch%u7B97%u6CD5%u6BD4%u8F83%uFF0C%u66F4%u591A%u8FC2%u56DE%u5A49%u8F6C%uFF0C%u96BE%u4E8E%u6536%u655B%u3002%u6240%u4EE5%u5728%u53D6%24%5Calpha%24%u7684%u65F6%u5019%u8981%u683C%u5916%u5C0F%u5FC3%u3002%u4E00%u822C%u91C7%u7528%u52A8%u6001%u8C03%u6574%u7684%u529E%u6CD5%u8BBE%u7F6E%24%5Calpha%24%0A%0A%3ELearning%20rate%20%24%5Calpha%24%20is%20typically%20held%20constant.%20Can%20slowly%20decrease%20%24%5Calpha%24%20over%20timee%20if%20we%20want%20%24%5Ctheta%24%20to%20converge.%0A%3E%24%24Eg%3A%20%5Calpha%20%3D%20%5Cfrac%20%7Bconst1%7D%7BiterationNumber%20+%20const2%7D%24%24%0A%0A%23%23Online%20learning%0A%u8FD9%u5176%u5B9E%u662Fstochastic%u7B97%u6CD5%u7684%u4E00%u4E2A%u5E94%u7528%u3002%u56E0%u4E3A%u5728%u6BCF%u4E00%u6B21%u7684%u8FED%u4EE3%u4E2D%uFF0C%u4E0D%u9700%u8981%u6240%u6709%u7684sample%u90FD%u53C2%u4E0E%uFF0C%u6240%u4EE5%u7B97%u6CD5%u53EF%u4EE5%u4E00%u8FB9%u624B%u673A%u6570%u636E%uFF0C%u4E00%u8FB9%u8FDB%u884C%u8FED%u4EE3%u4F18%u5316%u3002%0A%0A%23%23Map-reduce%20and%20data%20parallelism%0A%21%5BAlt%20text%5D%28./1498184980549.png%29%0A%u6240%u8C13%u7684map-reduce%u4E5F%u5C31%u662F%u5206%u5E03%u5F0F%u8FD0%u7B97%uFF0C%u51CF%u8F7B%u4E00%u5904%u7684%u8BA1%u7B97%u538B%u529B%uFF0C%u4FBF%u4E8E%u96C6%u7FA4%u8FD0%u7B97%u3002

Edit

Clustering - K-means Algorithm

步骤

  1. Randomly initialize K cluster centroids .
    1) Should have
    2) Randomly pick K training examples
    3) Set euqal to these K examples.
  2. Repeat
    for i=1 to m
    := index (from 1 to K) of cluster centroid closest to x^{(i)}
    归到离其最近的centroid
    for k = 1 to K
    := average (mean) of points assigned to cluster k.
    按照归到该centroid的所有sample来相应地移动该centroid

调优

目的

为了避免因为初始化不当,而产生local optima

Cost Function


调优就是使Cost Function最小化。具体的做法就是多做几次K-means (Andrew推荐100次),选取Cost Function最小的一组。

选择cluster number

Elvow method (肘关节法)

Dimensionality Reduction - Principal Component Analysis (PCA)

Dimensionality Reduction = 降维, 3D->2D

步骤

  1. Feature scaling/mean normalization.
    1)
    2)
    3) (optional) 是标准差
  2. Compute “covariance matrix”
  3. 调用系统库,Compute “eigenvectors” of matrix : [U,S,V] = svd(Sigma);
  4. Ureduce = U(:,1:k); z = Ureduce'*x;

Reconstruct

x = Ureduce * z

Choose k

Typically, choose k to be smallest value so that,

0.01 means 99% variance is retained. 保留了99%的变化。

另外一种通过svd函数计算variance的方法

  1. Start from k=1
  2. [U,S,V] = svd(Sigma)
  3. Pick the smallest k.

Application of PCA

  • Compression: 节省数据空间,提升计算速度
  • Visualization: k=2 or k = 3

不要滥用PCA。例如,用PCA减少feature set来消除Overfitting
Before implementing PCA, first try running whatever you want to do with the original/raw data . Only if that doesn’t do what you want, then implement PCA and consider using

Anomaly detection algorithm

我将之翻译为异常检测。实际就是对属于正态(或高斯)分布的特征量的非正常检测。

  1. Choose features that you think might be indicative of anomalous examples.
  2. Fit parameters
  3. Given new example , compute - 概率
  4. Anomaly if

高斯分布概率密度公式:

结果评估

如何对数据分类进行training和validate

Aircraft engines motivating example
10000 good(normal) engines
20 flawed engines (anomalous)

Training set: 6000 good engines
CV: 2000 good engines(y=0), 10 anomalous(y=1)
Test: 2000 good engines(y=0), 10 anomalous(y=1)
也就是把anomalous的sample省下来做validate和test用

特征量的选取

如果特征量不符合正态分布怎么办?按Andrew的建议是进行变换,则能得到近似正态的分布,如下:

Andrew在视频中用Octave命令行做了live demo:

讲义中提到的Error Analysis指的是:
有一些异常情况是,特征向量的各分量都在大概率的点,但是当某些分量组合时,会是小概率事件,则此时需要产生新的特征向量。例如:Andrew的例子,当CPU load相对较大,但仍在正常范围,但是network traffic却很小,但仍然在正常范围。这时你就需要一个变量,比如或者取决于新的特征量是否是正态分布。

Multivariate Gaussian distribution

前面的Anomaly Detection基于各特征分量是独立分布的情况,如果是非独立分布就采用上面提到的Error Analysis的方法。而Multivariate Gaussian则是对非独立分布的补充,也就是说上面的Anomaly Detection其实是Multivariate Gaussian的一种特殊情况。

Andrew给出了矢量化的Multivariate Gaussian的概率密度公式:


其中称为Covariance Matrix,协方差矩阵,的行列式(determinant),参考:

在上面给出的公式里,Covariance Matrix即就给出了变量之间的相关性的信息。

Original model和multivariate model都可以检测到上面举的CPU load和network traffic的例子中的情况。那要如何选择呢?Andrew给出了对比:

Anomaly detection vs. supervised learning

Recommender Systems

Content-based recommender systems

这个问题,其实就是已知每部电影的特征(特征向量),以及用户对电影的评分,来推测用户对没看过的电影的评分。看下面的表格:

其中:

  • : 用户数
  • : 电影数
  • : 用户j给电影i评分了
  • : 用户的真实评分
  • : hypothesis,推测评分

Gradient Descent:
for k = 0:

for k 0:

Collaborative filtering

假设在前面一节的training中,是已知的,而未知,即每部电影针对各个特征量的评分未知,此时通过training一样可以推测出各电影的评级。而Collabrative Filtering就是在两手空空的时候,既没有,也没有,而只有,就能预测

  1. Initialize to small random values.
  2. Minimize using gradient descent (or an advanced optimization algorithm).

  1. For a user with parameters and a movie with (learned) features , predict a star rating of

因为存在未给任何电影评分的用户,如果不做特殊处理,则这些用户对所有电影的预测评价都会是0分,具体Andrew在视频中给出论证。所以需要mean normalization,具体做法是:



  • 不用标准差除,因为评分本来就在近似的range

  • 1

Collaborative filtering矢量写法是:

而Collaborative filtering algorithm又叫Low rank matrix factorization。其中,就是Low rank matrix。

当要给用户推荐的时候,足够小的时候,就可以认为i和j足够相似。

%23Machine%20Learning%20%284%29%20-%20Unsupervised%20Learning%0A@%28%u5B66%u4E60%u7B14%u8BB0%29%5B%u6DF1%u5EA6%u5B66%u4E60%5D%0A%0A%5BTOC%5D%0A%0A%23%23Clustering%20-%20K-means%20Algorithm%0A%21%5BAlt%20text%7C400x0%5D%28./1495320402756.png%29%0A%21%5BAlt%20text%7C400x0%5D%28./1495320502218.png%29%0A%u6240%u8C13%u7684K-means%u5C31%u662F%u521D%u59CB%u5316%u4E24%u4E2Acluster%20centroid%uFF0C%u7136%u540E%u901A%u8FC7%u4E0D%u65AD%u8FED%u4EE3%uFF0C%u5C06%u4E4B%u79FB%u5230%u7C07%u7684%u4E2D%u592E%uFF0C%u4ECE%u800C%u8FBE%u5230%u5206%u7C7B%u3002%0A%0A%23%23%23%u6B65%u9AA4%0A1.%20Randomly%20initialize%20K%20cluster%20centroids%20.%0A%091%29%20Should%20have%20%24K%20%5Clt%20m%24%0A%092%29%20Randomly%20pick%20K%20training%20examples%0A%093%29%20Set%20%24%5Cmu_1%2C%20%5Cmu_2%2C...%2C%20%5Cmu_K%24%20euqal%20to%20these%20K%20examples.%0A2.%20Repeat%0A%09for%20i%3D1%20to%20m%0A%09%09%24c%5E%7B%28i%29%7D%24%20%3A%3D%20index%20%28from%201%20to%20K%29%20of%20cluster%20centroid%20closest%20to%20x%5E%7B%28i%29%7D%0A%09%09%u5C06%24x%5E%7B%28i%29%7D%24%u5F52%u5230%u79BB%u5176%u6700%u8FD1%u7684centroid%0A%09for%20k%20%3D%201%20to%20K%0A%09%09%24%5Cmu_k%24%20%3A%3D%20average%20%28mean%29%20of%20points%20assigned%20to%20cluster%20k.%0A%09%09%u6309%u7167%u5F52%u5230%u8BE5centroid%u7684%u6240%u6709sample%u6765%u76F8%u5E94%u5730%u79FB%u52A8%u8BE5centroid%0A%0A%23%23%23%u8C03%u4F18%0A%23%23%23%23%u76EE%u7684%0A%u4E3A%u4E86%u907F%u514D%u56E0%u4E3A%u521D%u59CB%u5316%u4E0D%u5F53%uFF0C%u800C%u4EA7%u751Flocal%20optima%0A%23%23%23%23Cost%20Function%0A%24J%28c%5E%7B%281%29%7D%2C%20c%5E%7B%282%29%7D%2C%20...%2C%20c%5E%7B%28m%29%7D%2C%20%5Cmu_1%2C%20%5Cmu_2%2C%20...%20%2C%5Cmu_K%29%20%3D%20%5Cdfrac%20%7B1%7D%7Bm%7D%20%5Csum_%7Bi%3D1%7D%5Em%7C%7Cx%5E%7B%28i%29%7D-%5Cmu_%7Bc%5E%7B%28i%29%7D%7D%7C%7C%5E2%24%0A%u8C03%u4F18%u5C31%u662F%u4F7FCost%20Function%u6700%u5C0F%u5316%u3002%u5177%u4F53%u7684%u505A%u6CD5%u5C31%u662F%u591A%u505A%u51E0%u6B21K-means%20%28Andrew%u63A8%u8350100%u6B21%29%uFF0C%u9009%u53D6Cost%20Function%u6700%u5C0F%u7684%u4E00%u7EC4%u3002%0A%0A%23%23%23%u9009%u62E9cluster%20number%0AElvow%20method%20%28%u8098%u5173%u8282%u6CD5%29%0A%21%5BAlt%20text%5D%28./1496198213650.png%29%0A%u4F46%u5982%u679C%u51FA%u73B0%u4E0B%u9762%u7684%u56FE%u50CF%uFF0C%u8098%u5173%u8282%u6CD5%u5C31%u5931%u6548%u4E86%0A%21%5BAlt%20text%5D%28./1496198240034.png%29%0A%u6309%u7167Andrew%u7684%u5EFA%u8BAE%uFF0C%u8FD8%u662F%u9700%u8981%u9886%u57DF%u903B%u8F91%u6765%u5224%u65AD%u9700%u8981%u5206%u4F5C%u51E0%u7C7B%u3002It%20depends%20on%20how%20to%20make%20the%20down%20stream%20or%20later%20process%20happy.%0A%0A%23%23Dimensionality%20Reduction%20-%20Principal%20Component%20Analysis%20%28PCA%29%0ADimensionality%20Reduction%20%3D%20%u964D%u7EF4%2C%203D-%3E2D%0A%23%23%23%u6B65%u9AA4%0A1.%20Feature%20scaling/mean%20normalization.%0A%091%29%20%24%5Cmu_i%20%3D%20%5Cdfrac%7B1%7D%7Bm%7D%5Csum_%7Bi%3D1%7D%5Emx%5E%7B%28i%29%7D_j%24%0A%092%29%20%24x_j%5E%7B%28i%29%7D%3Dx_j%5E%7B%28i%29%7D-%5Cmu_j%24%20%0A%093%29%20%28optional%29%20%24x_j%5E%7B%28i%29%7D%20%3D%20%5Cdfrac%20%7Bx_j%5E%7B%28i%29%7D-%5Cmu_j%7D%7Bs_j%7D%24%20%uFF0C%24s_j%24%u662F%u6807%u51C6%u5DEE%0A2.%20Compute%20%u201Ccovariance%20matrix%u201D%0A%24%24%5CSigma%20%3D%20%5Cdfrac%7B1%7D%7Bm%7D%5Csum_%7Bi%3D1%7D%5En%28x%5E%7B%28i%29%7D%29%28x%5E%7B%28i%29%7D%29%5ET%24%24%0A3.%20%u8C03%u7528%u7CFB%u7EDF%u5E93%uFF0CCompute%20%u201Ceigenvectors%u201D%20of%20matrix%20%24%5CSigma%24%3A%20%60%5BU%2CS%2CV%5D%20%3D%20svd%28Sigma%29%3B%60%0A4.%20%60Ureduce%20%3D%20U%28%3A%2C1%3Ak%29%3B%20z%20%3D%20Ureduce%27*x%3B%60%0A%0A%23%23%23Reconstruct%0A%60x%20%3D%20Ureduce%20*%20z%60%0A%0A%21%5BAlt%20text%5D%28./1496209550430.png%29%0A%0A%0A%23%23%23%20Choose%20k%0ATypically%2C%20choose%20k%20to%20be%20smallest%20value%20so%20that%2C%0A%24%24%5Cdfrac%20%7B%5Cdfrac%20%7B1%7D%7Bm%7D%7C%7Cx%5E%7B%28i%29%7D-x_%7Bapprox%7D%5E%7B%28i%29%7D%7C%7C%5E2%7D%7B%5Cdfrac%20%7B1%7D%7Bm%7D%7C%7Cx%5E%7B%28i%29%7D%7C%7C%5E2%7D%20%5Cleq%200.01%24%24%0A0.01%20means%2099%25%20variance%20is%20retained.%20%u4FDD%u7559%u4E8699%25%u7684%u53D8%u5316%u3002%0A%0A%u53E6%u5916%u4E00%u79CD%u901A%u8FC7svd%u51FD%u6570%u8BA1%u7B97variance%u7684%u65B9%u6CD5%0A1.%20Start%20from%20k%3D1%0A2.%20%60%5BU%2CS%2CV%5D%20%3D%20svd%28Sigma%29%60%0A2.%20%241%20-%20%5Cdfrac%20%7B%5Csum_%7Bi%3D1%7D%5EkS_%7Bii%7D%7D%7B%5Csum_%7Bi%3D1%7D%5EnS_%7Bii%7D%7D%20%5Cleq%200.01%24%0A3.%20Pick%20the%20smallest%20k.%0A%0A%23%23%23Application%20of%20PCA%0A-%20Compression%3A%20%u8282%u7701%u6570%u636E%u7A7A%u95F4%uFF0C%u63D0%u5347%u8BA1%u7B97%u901F%u5EA6%0A-%20Visualization%3A%20k%3D2%20or%20k%20%3D%203%0A%0A%u4E0D%u8981%u6EE5%u7528PCA%u3002%u4F8B%u5982%uFF0C%u7528PCA%u51CF%u5C11feature%20set%u6765%u6D88%u9664Overfitting%0ABefore%20implementing%20PCA%2C%20first%20try%20running%20whatever%20you%20want%20to%20do%20with%20the%20original/raw%20data%20%24x%5E%7B%28i%29%7D%24.%20Only%20if%20that%20doesn%u2019t%20do%20what%20you%20want%2C%20then%20implement%20PCA%20and%20consider%20using%20%24z%5E%7B%28i%29%7D%24%0A%0A%23%23Anomaly%20detection%20algorithm%0A%u6211%u5C06%u4E4B%u7FFB%u8BD1%u4E3A%u5F02%u5E38%u68C0%u6D4B%u3002%u5B9E%u9645%u5C31%u662F%u5BF9%u5C5E%u4E8E%u6B63%u6001%uFF08%u6216%u9AD8%u65AF%uFF09%u5206%u5E03%u7684%u7279%u5F81%u91CF%u7684%u975E%u6B63%u5E38%u68C0%u6D4B%u3002%0A1.%20Choose%20features%20%24x_i%24%20that%20you%20think%20might%20be%20indicative%20of%20anomalous%20examples.%0A2.%20Fit%20parameters%20%24%5Cmu_1%2C%20...%2C%5Cmu_n%2C%20%5Csigma_1%2C...%2C%5Csigma_n%24%0A%09%24%24%5Cmu_j%20%3D%20%5Cdfrac%20%7B1%7D%7Bm%7D%20%5Csum_%7Bi%3D1%7D%5Em%20x_j%5E%7B%28i%29%7D%24%24%0A%09%24%24%5Csigma_j%5E2%20%3D%20%5Cdfrac%20%7B1%7D%7Bm%7D%20%5Csum_%7Bi%3D1%7D%5Em%20%28x_j%5E%7B%28i%29%7D%20-%20%5Cmu_j%29%5E2%24%24%0A3.%20Given%20new%20example%20%24x%24%2C%20compute%20%24p%28x%29%24%20-%20%u6982%u7387%0A%09%24%24p%28x%29%20%3D%20%5Cprod_%7Bj%3D1%7D%5En%20p%28x_j%3B%20%5Cmu_j%2C%20%5Csigma_j%5E2%29%20%3D%20%5Cprod_%7Bj%3D1%7D%5En%20%5Cdfrac%20%7B1%7D%7B%5Csqrt%20%7B2%5Cpi%7D%5Csigma_j%7Dexp%20%5Cleft%28%7B-%5Cdfrac%20%7B%28x_j-%5Cmu_j%29%5E2%7D%7B2%20%5Csigma_j%5E2%7D%7D%5Cright%29%24%24%0A4.%20%20Anomaly%20if%20%24p%28x%29%20%5Cle%20%5Cepsilon%24%0A%0A%u9AD8%u65AF%u5206%u5E03%u6982%u7387%u5BC6%u5EA6%u516C%u5F0F%uFF1A%0A%24%24p%28x%29%20%3D%20%5Cdfrac%20%7B1%7D%7B%5Csqrt%20%7B2%5Cpi%7D%5Csigma%7Dexp%20%5Cleft%28%7B-%5Cdfrac%20%7B%28x-%5Cmu%29%5E2%7D%7B2%20%5Csigma%5E2%7D%7D%5Cright%29%24%24%0A%0A%23%23%23%u7ED3%u679C%u8BC4%u4F30%0A%u5982%u4F55%u5BF9%u6570%u636E%u5206%u7C7B%u8FDB%u884Ctraining%u548Cvalidate%0A%0A%3EAircraft%20engines%20motivating%20example%20%0A10000%20%20good%28normal%29%20engines%0A20%20%20flawed%20engines%20%28anomalous%29%0A%0A%3ETraining%20set%3A%206000%20good%20engines%0ACV%3A%202000%20good%20engines%28y%3D0%29%2C%2010%20anomalous%28y%3D1%29%0ATest%3A%202000%20good%20engines%28y%3D0%29%2C%2010%20anomalous%28y%3D1%29%0A%u4E5F%u5C31%u662F%u628Aanomalous%u7684sample%u7701%u4E0B%u6765%u505Avalidate%u548Ctest%u7528%0A%23%23%23%u7279%u5F81%u91CF%u7684%u9009%u53D6%0A%u5982%u679C%u7279%u5F81%u91CF%u4E0D%u7B26%u5408%u6B63%u6001%u5206%u5E03%u600E%u4E48%u529E%uFF1F%u6309Andrew%u7684%u5EFA%u8BAE%u662F%u8FDB%u884C%u53D8%u6362%uFF0C%u5219%u80FD%u5F97%u5230%u8FD1%u4F3C%u6B63%u6001%u7684%u5206%u5E03%uFF0C%u5982%u4E0B%uFF1A%0A%21%5BAlt%20text%5D%28./1496744051794.png%29%0A%u4E00%u822C%u7528%u5230%u7684%u65B9%u6CD5%u6709%uFF1A%0A%24x_%7Bnew%7D%20%3D%20log%28x+c%29%24%0A%24x_%7Bnew%7D%20%3D%20x%5E%7B%5Cfrac%20%7B1%7D%7Bn%7D%7D%24%0A%0AAndrew%u5728%u89C6%u9891%u4E2D%u7528Octave%u547D%u4EE4%u884C%u505A%u4E86live%20demo%uFF1A%0A%21%5BAlt%20text%5D%28./1496744359543.png%29%0A%u6211%u4EEC%u5F80%u5F80%u53EF%u4EE5%u901A%u8FC7hist%u547D%u4EE4%u6765%u753B%u56FE%u67E5%u770B%u4FEE%u6B63%u7684%u7279%u5F81%u91CF%u662F%u5426%u5C5E%u4E8E%u6B63%u6001%u5206%u5E03%u3002%0A%0A%u8BB2%u4E49%u4E2D%u63D0%u5230%u7684**Error%20Analysis**%u6307%u7684%u662F%uFF1A%0A%u6709%u4E00%u4E9B%u5F02%u5E38%u60C5%u51B5%u662F%uFF0C%u7279%u5F81%u5411%u91CF%u7684%u5404%u5206%u91CF%u90FD%u5728%u5927%u6982%u7387%u7684%u70B9%uFF0C%u4F46%u662F%u5F53%u67D0%u4E9B%u5206%u91CF%u7EC4%u5408%u65F6%uFF0C%u4F1A%u662F%u5C0F%u6982%u7387%u4E8B%u4EF6%uFF0C%u5219%u6B64%u65F6%u9700%u8981%u4EA7%u751F%u65B0%u7684%u7279%u5F81%u5411%u91CF%u3002%u4F8B%u5982%uFF1AAndrew%u7684%u4F8B%u5B50%uFF0C%u5F53CPU%20load%u76F8%u5BF9%u8F83%u5927%uFF0C%u4F46%u4ECD%u5728%u6B63%u5E38%u8303%u56F4%uFF0C%u4F46%u662Fnetwork%20traffic%u5374%u5F88%u5C0F%uFF0C%u4F46%u4ECD%u7136%u5728%u6B63%u5E38%u8303%u56F4%u3002%u8FD9%u65F6%u4F60%u5C31%u9700%u8981%u4E00%u4E2A%u53D8%u91CF%uFF0C%u6BD4%u5982%24x_5%20%3D%20%5Cfrac%20%7BCPU%5C%20load%7D%20%7Bnetwork%5C%20traffic%7D%24%u6216%u8005%24x_6%20%3D%20%5Cfrac%20%7B%28CPU%5C%20load%29%5E2%7D%7Bnetwork%5C%20traffic%7D%24%u53D6%u51B3%u4E8E%u65B0%u7684%u7279%u5F81%u91CF%u662F%u5426%u662F%u6B63%u6001%u5206%u5E03%u3002%0A%23%23%23Multivariate%20Gaussian%20distribution%0A%u524D%u9762%u7684Anomaly%20Detection%u57FA%u4E8E%u5404%u7279%u5F81%u5206%u91CF%u662F%u72EC%u7ACB%u5206%u5E03%u7684%u60C5%u51B5%uFF0C%u5982%u679C%u662F%u975E%u72EC%u7ACB%u5206%u5E03%u5C31%u91C7%u7528%u4E0A%u9762%u63D0%u5230%u7684**Error%20Analysis**%u7684%u65B9%u6CD5%u3002%u800CMultivariate%20Gaussian%u5219%u662F%u5BF9%u975E%u72EC%u7ACB%u5206%u5E03%u7684%u8865%u5145%uFF0C%u4E5F%u5C31%u662F%u8BF4%u4E0A%u9762%u7684Anomaly%20Detection%u5176%u5B9E%u662FMultivariate%20Gaussian%u7684%u4E00%u79CD%u7279%u6B8A%u60C5%u51B5%u3002%0A%0AAndrew%u7ED9%u51FA%u4E86%u77E2%u91CF%u5316%u7684Multivariate%20Gaussian%u7684%u6982%u7387%u5BC6%u5EA6%u516C%u5F0F%uFF1A%0A%24%24p%28x%3B%5Cmu%2C%20%5CSigma%29%20%3D%20%5Cdfrac%20%7B1%7D%7B%282%5Cpi%29%5E%7B%5Cfrac%20%7Bn%7D%7B2%7D%7D%5Cleft%20%7C%20%5CSigma%5Cright%7C%5E%7B%5Cfrac%7B1%7D%7B2%7D%7D%7Dexp%20%5Cleft%28-%5Cfrac%7B1%7D%7B2%7D%28x-%5Cmu%29%5ET%5CSigma%5E%7B-1%7D%28x-%5Cmu%29%5Cright%29%24%24%0A%24%5Cmu%20%3D%20%5Cfrac%20%7B1%7D%7Bm%7D%5Csum_%7Bi%3D1%7D%5Em%20x%5E%7B%28i%29%7D%24%0A%24%5CSigma%20%3D%20%5Cfrac%20%7B1%7D%7Bm%7D%5Csum_%7Bi%3D1%7D%5Em%20%28x%5E%7B%28i%29%7D-%5Cmu%29%28x%5E%7B%28i%29%7D-%5Cmu%29%5ET%24%0A%3E%20%0A%u5176%u4E2D%24%5CSigma%24%u79F0%u4E3ACovariance%20Matrix%uFF0C%u534F%u65B9%u5DEE%u77E9%u9635%uFF0C%24%7C%5CSigma%7C%24%u4E3A%24%5CSigma%24%u7684%u884C%u5217%u5F0F%28determinant%29%uFF0C%u53C2%u8003%uFF1A%0A-%20%5Bwiki%20-%20%u884C%u5217%u5F0F%5D%28https%3A//zh.wikipedia.org/wiki/%25E8%25A1%258C%25E5%2588%2597%25E5%25BC%258F%29%0A%u7B80%u5355%u6458%u53D6%u4E24%u5F20%u8BA1%u7B97%u77E9%u9635%u884C%u5217%u5F0F%u7684%u63D2%u56FE%uFF1A%0A%3E%21%5BAlt%20text%5D%28./1496745897356.png%29%0A%3E%0A%3E%21%5BAlt%20text%5D%28./1496745910661.png%29%0A%3EOctave%u91CC%u9762%u7528%u51FD%u6570%60det%28A%29%60%0A%0A%u5728%u4E0A%u9762%u7ED9%u51FA%u7684%u516C%u5F0F%u91CC%uFF0CCovariance%20Matrix%u5373%24%5CSigma%24%u5C31%u7ED9%u51FA%u4E86%u53D8%u91CF%u4E4B%u95F4%u7684%u76F8%u5173%u6027%u7684%u4FE1%u606F%u3002%0A%0AOriginal%20model%u548Cmultivariate%20model%u90FD%u53EF%u4EE5%u68C0%u6D4B%u5230%u4E0A%u9762%u4E3E%u7684CPU%20load%u548Cnetwork%20traffic%u7684%u4F8B%u5B50%u4E2D%u7684%u60C5%u51B5%u3002%u90A3%u8981%u5982%u4F55%u9009%u62E9%u5462%uFF1FAndrew%u7ED9%u51FA%u4E86%u5BF9%u6BD4%uFF1A%0A%21%5BAlt%20text%5D%28./1496746985199.png%29%0A%u53EF%u89C1%u867D%u7136%u770B%u8D77%u6765Multivariate%u6B63%u89C4%u4E00%u4E9B%uFF0C%u4F46%u662FOriginal%20model%u66F4%u5B9E%u7528%u4E00%u70B9%u3002%0A%23%23%23Anomaly%20detection%20vs.%20supervised%20learning%0A%21%5BAlt%20text%5D%28./1496580685327.png%29%0A%21%5BAlt%20text%5D%28./1496580714267.png%29%0A%0A%23%23Recommender%20Systems%0A%23%23%23Content-based%20recommender%20systems%0A%u8FD9%u4E2A%u95EE%u9898%uFF0C%u5176%u5B9E%u5C31%u662F%u5DF2%u77E5%u6BCF%u90E8%u7535%u5F71%u7684%u7279%u5F81%28%u7279%u5F81%u5411%u91CF%29%uFF0C%u4EE5%u53CA%u7528%u6237%u5BF9%u7535%u5F71%u7684%u8BC4%u5206%uFF0C%u6765%u63A8%u6D4B%u7528%u6237%u5BF9%u6CA1%u770B%u8FC7%u7684%u7535%u5F71%u7684%u8BC4%u5206%u3002%u770B%u4E0B%u9762%u7684%u8868%u683C%uFF1A%0A%21%5BAlt%20text%5D%28./1497231459572.png%29%0A%u65B9%u6CD5%u5C31%u662F%u7EBF%u6027%u56DE%u5F52%uFF0C%u6C42%u4E0B%u9762cost%20function%u7684%u6700%u5C0F%u503C%uFF1A%0A%24%24min_%7B%5Ctheta%5E%7B%281%29%7D%2C%20%5Ctheta%5E%7B%282%29%7D%2C...%2C%20%5Ctheta%5E%7B%28n_u%29%7D%7D%5Cfrac%7B1%7D%7B2%7D%5Csum_%7Bj%3D1%7D%5E%7Bn_u%7D%5Csum_%7Bi%3Ar%28i%2Cj%29%3D1%7D%5Cleft%20%28%28%5Ctheta%5E%7B%28j%29%7D%29%5ETx%5E%7B%28i%29%7D%20-%20y%5E%7B%28i%2Cj%29%7D%5Cright%29%5E2%20+%20%5Cfrac%7B%5Clambda%7D%7B2%7D%5Csum_%7Bj%3D1%7D%5E%7Bn_u%7D%5Csum_%7Bk%3D1%7D%5En%5Cleft%28%5Ctheta_k%5E%7B%28j%29%7D%5Cright%29%5E2%24%24%0A%u5176%u4E2D%uFF1A%0A-%20%24n_u%24%3A%20%u7528%u6237%u6570%0A-%20%24n%24%3A%20%u7535%u5F71%u6570%0A-%20%24i%3Ar%28i%2Cj%29%20%3D%201%24%3A%20%u7528%u6237j%u7ED9%u7535%u5F71i%u8BC4%u5206%u4E86%0A-%20%24y%5E%7B%28i%2Cj%29%7D%24%3A%20%u7528%u6237%u7684%u771F%u5B9E%u8BC4%u5206%0A-%20%24%28%5Ctheta%5E%7B%28j%29%7D%29%5ETx%5E%7B%28i%29%7D%24%3A%20hypothesis%uFF0C%u63A8%u6D4B%u8BC4%u5206%0A%0A**Gradient%20Descent**%3A%0Afor%20k%20%3D%200%3A%0A%24%5Ctheta_k%5E%7B%28j%29%7D%20%3A%3D%20%5Ctheta_k%5E%7B%28j%29%7D%20-%20%5Calpha%5Csum_%7Bi%3Ar%28i%2Cj%29%3D1%7D%5Cleft%28%28%5Ctheta%5E%7B%28j%29%7D%29%5ETx%5E%7B%28i%29%7D-y%5E%7B%28i%2Cj%29%7D%5Cright%29x_k%5E%7B%28i%29%7D%24%20%0Afor%20k%24%5Cneq%24%200%3A%0A%24%5Ctheta_k%5E%7B%28j%29%7D%20%3A%3D%20%5Ctheta_k%5E%7B%28j%29%7D%20-%20%5Calpha%5Cleft%28%5Csum_%7Bi%3Ar%28i%2Cj%29%3D1%7D%5Cleft%28%28%5Ctheta%5E%7B%28j%29%7D%29%5ETx%5E%7B%28i%29%7D-y%5E%7B%28i%2Cj%29%7D%5Cright%29x_k%5E%7B%28i%29%7D%20+%20%5Clambda%5Ctheta_k%5E%7B%28j%29%7D%5Cright%29%24%20%0A%0A%23%23%23Collaborative%20filtering%0A%u5047%u8BBE%u5728%u524D%u9762%u4E00%u8282%u7684training%u4E2D%uFF0C%24%5Ctheta_k%5E%7B%28j%29%7D%24%u662F%u5DF2%u77E5%u7684%uFF0C%u800C%24x%5E%7B%28i%29%7D%24%u672A%u77E5%uFF0C%u5373%u6BCF%u90E8%u7535%u5F71%u9488%u5BF9%u5404%u4E2A%u7279%u5F81%u91CF%u7684%u8BC4%u5206%u672A%u77E5%uFF0C%u6B64%u65F6%u901A%u8FC7training%u4E00%u6837%u53EF%u4EE5%u63A8%u6D4B%u51FA%u5404%u7535%u5F71%u7684%u8BC4%u7EA7%u3002%u800CCollabrative%20Filtering%u5C31%u662F%u5728%u4E24%u624B%u7A7A%u7A7A%u7684%u65F6%u5019%uFF0C%u65E2%u6CA1%u6709%24%5Ctheta%24%uFF0C%u4E5F%u6CA1%u6709%24x%24%uFF0C%u800C%u53EA%u6709%24y%5E%7B%28i%2Cj%29%7D%24%uFF0C%u5C31%u80FD%u9884%u6D4B%24%5Ctheta%24%u548C%24x%24%u3002%0A%0A1.%20Initialize%20%24x%5E%7B%281%29%7D%2C%20x%5E%7B%281%29%7D%2C%20...%2C%20x%5E%7B%28n_m%29%7D%2C%20%5Ctheta%5E%7B%281%29%7D%20%2C%20%5Ctheta%5E%7B%282%29%7D%2C...%2C%5Ctheta%5E%7B%28n_u%29%7D%24%20to%20small%20random%20values.%0A2.%20Minimize%20%24J%28x%5E%7B%281%29%7D%2C%20x%5E%7B%281%29%7D%2C%20...%2C%20x%5E%7B%28n_m%29%7D%2C%20%5Ctheta%5E%7B%281%29%7D%20%2C%20%5Ctheta%5E%7B%282%29%7D%2C...%2C%5Ctheta%5E%7B%28n_u%29%7D%29%24using%20gradient%20descent%20%28or%20an%20advanced%20optimization%20algorithm%29.%0A%24J%20%3D%20%5Cfrac%7B1%7D%7B2%7D%5Csum_%7B%28i%2Cj%29%3Ar%28i%2Cj%29%3D1%7D%5Cleft%28%28%5Ctheta%5E%7B%28j%29%7D%29%5ETx%5E%7B%28i%29%7D%20-%20y%5E%7B%28i%2Cj%29%7D%5Cright%29%5E2%20+%20%5Cfrac%7B%5Clambda%7D%7B2%7D%5Csum_%7Bi%3D1%7D%5E%7Bn_m%7D%5Csum_%7Bk%3D1%7D%5En%5Cleft%28x_k%5E%7B%28i%29%7D%5Cright%29%5E2+%5Cfrac%7B%5Clambda%7D%7B2%7D%5Csum_%7Bi%3D1%7D%5E%7Bn_m%7D%5Csum_%7Bk%3D1%7D%5En%5Cleft%28%5Ctheta_k%5E%7B%28i%29%7D%5Cright%29%5E2%24%0A%21%5BAlt%20text%5D%28./1497233522468.png%29%0A3.%20For%20a%20user%20with%20parameters%20%24%5Ctheta%24%20and%20a%20movie%20with%20%28learned%29%20features%20%24x%24%2C%20predict%20a%20star%20rating%20of%20%24%5Ctheta%5ETx%24%0A%3E%u56E0%u4E3A%u5B58%u5728%u672A%u7ED9%u4EFB%u4F55%u7535%u5F71%u8BC4%u5206%u7684%u7528%u6237%uFF0C%u5982%u679C%u4E0D%u505A%u7279%u6B8A%u5904%u7406%uFF0C%u5219%u8FD9%u4E9B%u7528%u6237%u5BF9%u6240%u6709%u7535%u5F71%u7684%u9884%u6D4B%u8BC4%u4EF7%u90FD%u4F1A%u662F0%u5206%uFF0C%u5177%u4F53Andrew%u5728%u89C6%u9891%u4E2D%u7ED9%u51FA%u8BBA%u8BC1%u3002%u6240%u4EE5%u9700%u8981mean%20normalization%uFF0C%u5177%u4F53%u505A%u6CD5%u662F%uFF1A%0A%3E-%20%24%5Cmu%20%3D%20%5Cfrac%20%7B1%7D%7Bm%7D%20%5Csum_1%5Em%20y%5E%7Bi%7D%24%0A%3E-%20%24Y%20%3D%20Y%20-%20%5Cmu%24%0A%3E-%20%u4E0D%u7528%u6807%u51C6%u5DEE%u9664%uFF0C%u56E0%u4E3A%u8BC4%u5206%u672C%u6765%u5C31%u5728%u8FD1%u4F3C%u7684range%0A%0A1%0A%0A%0ACollaborative%20filtering%u77E2%u91CF%u5199%u6CD5%u662F%uFF1A%0A%24%24h_%7B%5CTheta%7D%20%3D%20X%5CTheta%5ET%24%24%0A%u800CCollaborative%20filtering%20algorithm%u53C8%u53EBLow%20rank%20matrix%20factorization%u3002%u5176%u4E2D%uFF0C%24X%5CTheta%5ET%24%u5C31%u662FLow%20rank%20matrix%u3002%0A%0A%3E%u5F53%u8981%u7ED9%u7528%u6237%u63A8%u8350%u7684%u65F6%u5019%uFF0C%24%7C%7Cx_j%20-%20x_i%7C%7C%24%u8DB3%u591F%u5C0F%u7684%u65F6%u5019%uFF0C%u5C31%u53EF%u4EE5%u8BA4%u4E3Ai%u548Cj%u8DB3%u591F%u76F8%u4F3C%u3002%0A%0A%0A%0A%0A%0A%0A%0A%09%09%0A

Edit


SVM中文译名:支持向量机

特点是large margin,相对logistic regression可以容易得到全局最优解。

工作原理

基于land marks,如下

Hypothesis

Logistic Regression的Hypothesis为

对以上公式的两部分进行拟合,参看下图:

SVM Decision Boundary

Kernel

Gaussian Kernel

Steps

  1. Given
  2. Choose
  3. x->f
  4. Predict’“y=1”’if

Note: Do perform feature scaling before using the Gaussian kernel.

Multiclass classification:
Use one vs. all method. (Train K SVMs, one to distinguish y= i from the rest, for i = 1, 2,…,K), get
对于新的输入x,选取使最大的i。

Parameters

C =
- Large C: small Lower’bias,’high’variance.
- Small C: big Higher’bias,’low’variance.

  • Large : Features vary more smoothly. Higher bias, lower variance.

  • Small : Features vary less smoothly. Lower bias, higher variance.

Andrew提到还有很多其他的Kernel,但是用处不是很多,包括:

  • Polynomial kernel
  • String’kernel
  • chiIsquare’kernel
  • histogram intersection kernel,

Logistic regression vs. SVMs

n = number of features (), m = number of training examples

  • If n is large (relative to m): Use logistic regression, or SVM without a kernel (“linear kernel”).
  • If n is small, m is intermediate: Use SVM with Gaussian kernel.
  • If n is small, m is large: Create/add more features, then use logistic regression or SVM without a kernel.
  • Neural network likely to work well for most of these settings, but may be slower to train.

Recommendation for SVM implemetation

LIBSVM

%23Machine%20Learning%20%283%29%20-%20Support%20Vector%20Machine%20%28SVM%29%0A@%28%u5B66%u4E60%u7B14%u8BB0%29%5B%u6DF1%u5EA6%u5B66%u4E60%5D%0ASVM%u4E2D%u6587%u8BD1%u540D%uFF1A%u652F%u6301%u5411%u91CF%u673A%0A%0A%u7279%u70B9%u662Flarge%20margin%uFF0C%u76F8%u5BF9logistic%20regression%u53EF%u4EE5%u5BB9%u6613%u5F97%u5230%u5168%u5C40%u6700%u4F18%u89E3%u3002%0A%0A%23%23%u5DE5%u4F5C%u539F%u7406%0A%u57FA%u4E8Eland%20marks%uFF0C%u5982%u4E0B%0A%21%5BAlt%20text%7C600x0%5D%28./1494841637067.png%29%0A%u5F53%u8BAD%u7EC3%u51FA%24%5Ctheta%24%u503C%u5982%u4E0A%u6240%u793A%uFF0C%u5F53X%u53D6%u503C%u5728%24l%5E%7B%281%29%7D%24%2C%20%24l%5E%7B%282%29%7D%24%u9644%u8FD1%u65F6%uFF0Cy%u9884%u6D4B%u4E3A1%uFF0C%u5F53%u5728%24l%5E%7B%283%29%7D%24%u9644%u8FD1%u65F6%u4E3A0%u3002%u600E%u4E48%u505A%u5230%u7684%u5462%uFF1F%0A%23%23%23Hypothesis%0ALogistic%20Regression%u7684Hypothesis%u4E3A%0A%24h_%7B%5Ctheta%7D%28x%29%20%3D%20%20-y%20%5Cdfrac%20%7B1%7D%7B1+e%5E%7B-%5Ctheta%5ETx%7D%7D%20-%20%281-y%29%5Cdfrac%20%7B1%7D%7B1+e%5E%7B-%5Ctheta%5ETx%7D%7D%24%0A%0A%u5BF9%u4EE5%u4E0A%u516C%u5F0F%u7684%u4E24%u90E8%u5206%u8FDB%u884C%u62DF%u5408%uFF0C%u53C2%u770B%u4E0B%u56FE%uFF1A%0A%21%5BAlt%20text%7C600x0%5D%28./1494842281122.png%29%0A%u6709%u7528%u7684%u90E8%u5206%u5C31%u662Fz%u8F74%u4E0A%u90A3%u4E00%u7AEF%uFF0C%u659C%u7EBF%u90E8%u5206%u659C%u7387%u65E0%u5173%u7D27%u8981%0A%0ASVM%20Decision%20Boundary%0A%24min_%5Ctheta%20C%5Csum_%7Bi%3D1%7D%5E%7Bm%7D%5By%5E%7B%28i%29%7Dcost_1%28%5Ctheta%5ETx%5E%7B%28i%29%7D%29%20+%20%281-y%5E%7B%28i%29%7D%29cost_0%28%5Ctheta%5ETx%5E%7B%28i%29%7D%29%5D%20+%20%5Cdfrac%7B1%7D%7B2%7D%5Csum_%7Bi%3D1%7D%5E%7Bn%7D%5Ctheta_j%5E2%24%0A%24C%3D%5Cdfrac%7B1%7D%7B%5Clambda%7D%24%0A%0A%23%23%23Kernel%0AGaussian%20Kernel%0A%24f_i%20%3D%20similarity%28x%2C%20l%5E%7B%28i%29%7D%29%20%3D%20exp%28-%5Cdfrac%20%7B%7C%7Cx-l%5E%7B%28i%29%7D%7C%7C%5E2%7D%7B2%5Csigma%5E2%7D%29%24%0A%0A%23%23%23Steps%0A1.%20Given%24%20%28x%5E%7B%281%29%7D%2C%20y%5E%7B%281%29%7D%29%2C%20%28x%5E%7B%282%29%7D%2C%20y%5E%7B%282%29%7D%29%2C...%2C%28x%5E%7B%28m%29%7D%2C%20y%5E%7B%28m%29%7D%29%24%0A2.%20Choose%20%24l%5E%7B%281%29%7D%20%3D%20x%5E%7B%281%29%7D%2C%20l%5E%7B%282%29%7D%20%3D%20x%5E%7B%282%29%7D%2C%20...%2Cl%5E%7B%28m%29%7D%20%3D%20x%5E%7B%28m%29%7D%24%0A3.%20x-%3Ef%0A4.%20Predict%27%u201Cy%3D1%u201D%27if%20%24%5Ctheta%5ETf%20%5Cgeq%200%24%0A%0A**Note**%3A%20Do%20perform%20feature%20scaling%20before%20using%20the%20Gaussian%20kernel.%0A%0AMulticlass%20classification%3A%0AUse%20one%20vs.%20all%20method.%20%28Train%20K%20SVMs%2C%20one%20to%20distinguish%20y%3D%20i%20from%20the%20rest%2C%20for%20i%20%3D%201%2C%202%2C...%2CK%29%2C%20get%20%24%5Ctheta%5E%7B%281%29%7D%2C%20%5Ctheta%5E%7B%282%29%7D%2C...%2C%5Ctheta%5E%7B%28K%29%7D%24%0A%u5BF9%u4E8E%u65B0%u7684%u8F93%u5165x%uFF0C%u9009%u53D6%u4F7F%24%28%5Ctheta%5E%7B%28i%29%7D%29%5ETx%24%u6700%u5927%u7684i%u3002%0A%0A%23%23%23Parameters%0AC%20%3D%20%24%5Cdfrac%20%7B1%7D%7B%5Clambda%7D%24%09%0A%09-%20Large%20C%3A%20small%20%24%5Clambda%24%20Lower%27bias%2C%27high%27variance.%0A%09-%20Small%20C%3A%20big%20%24%5Clambda%24%20Higher%27bias%2C%27low%27variance.%0A%0A%24%5Csigma%5E2%24%0A-%20Large%20%24%5Csigma%5E2%24%3A%20Features%20%24f_i%24%20vary%20more%20smoothly.%20Higher%20bias%2C%20lower%20variance.%0A%20%21%5BAlt%20text%5D%28./1494987748588.png%29%0A-%20Small%20%24%5Csigma%5E2%24%3A%20Features%20%24f_i%24%20vary%20less%20smoothly.%20Lower%20bias%2C%20higher%20variance.%0A%21%5BAlt%20text%5D%28./1494987812098.png%29%0A%0AAndrew%u63D0%u5230%u8FD8%u6709%u5F88%u591A%u5176%u4ED6%u7684Kernel%uFF0C%u4F46%u662F%u7528%u5904%u4E0D%u662F%u5F88%u591A%uFF0C%u5305%u62EC%uFF1A%0A-%20Polynomial%20kernel%0A-%20String%27kernel%0A-%20chiIsquare%27kernel%0A-%20histogram%20intersection%20kernel%2C%0A%0A%23%23Logistic%20regression%20vs.%20SVMs%0An%20%3D%20number%20of%20features%20%28%24x%20%5Cin%20R%5E%7Bn+1%7D%24%29%2C%20m%20%3D%20number%20of%20training%20examples%0A-%20If%20n%20is%20large%20%28relative%20to%20m%29%3A%20Use%20logistic%20regression%2C%20or%20SVM%20without%20a%20kernel%20%28%u201Clinear%20kernel%u201D%29.%0A-%20If%20n%20is%20small%2C%20m%20is%20intermediate%3A%20Use%20SVM%20with%20Gaussian%20kernel.%0A-%20If%20n%20is%20small%2C%20m%20is%20large%3A%20Create/add%20more%20features%2C%20then%20use%20logistic%20regression%20or%20SVM%20without%20a%20kernel.%0A-%20Neural%20network%20likely%20to%20work%20well%20for%20most%20of%20these%20settings%2C%20but%20may%20be%20slower%20to%20train.%0A%0A%23%23%20Recommendation%20for%20SVM%20implemetation%0A%5BLIBSVM%5D%28https%3A//www.csie.ntu.edu.tw/%7Ecjlin/libsvm/%29%0A%0A%0A%0A

Edit

Model

: activation of unit i in layer j
: matrix of weights controlling function mapping from layer j to layer j+1

这是一个三层神经网络。每一层都是遵循Logistic Regression的sigmoid function,所以是非线性的。


The sigmoid function is

通过hypothesis function 就可以计算cost function。公式在Machine Learning (1) - Linear & Logistic Regression
摘取公式如下:

Note:

  • 的尺寸为,取决于上一层和这一层的node数量。其中的+1,是bias节点,即常量(+1)节点,而不算在节点数中
  • 矩阵的数量取决于网络层数,即3层网络,只有2个矩阵

Cost Function

接着上一章的公式,给出完整的多状态分类并且正规化的cost function如下:

稍作解释:

  • 公式的前半部分是原版的hypothesis,并对所有的分类进行累加
  • 后半部分是正规化参数,是对所有的参数进行平方累加。一共有L-1层神经网络,每一层有


注意Regularized项,只累加项,不包含bias项

Backpropagation Algorithm

有了cost function,就可以开始Gradient descent了,基本公式如下:

但是现在引入了神经网络,求偏导数就没这么简单了,所以引入了Forwardpropagation & Backpropagation。意即,向前算一遍,再向回算一遍,综合两方面的数据就能得出cost function的偏导数。证明不会,但是Andrew给出了公式。
整个算法过程如下,这里不同于教材中的4层神经网络,而是采用本篇笔记中的三层神经网络:

Given training set
Set for all (l,i,j)。 size() = ,与
For i =1 to m
   1. Set , both a and x are vectors
   2. Perform forward propagation to compute for l=2,3,…,L. L=神经网络层数
         ——注意加入bias项
        
         ——注意加入bias项
        
        

   3. Using , compute . y是对输出的量测,是对输出的模型估计,算是模型偏差。
   4. Using backpropagation to compute ,输入层没有,即
     ——此处应当有误,具体参考相关笔记

注:
针对的计算要特别注意。下面专门放一节解释计算

   5. Compute
    
    Vectorized equation: ,其中j是每一层网络中的节点编号
注:
此处累加指的是针对training samples

   6. Final step, compute the partial derivative of
    
    
    

整个过程略显复杂,但是这就是神经网络。据说后面有更优雅的算法。

计算

: an error item that measures how much the node was responsible for any errors in our output.



最终计算式应为:

g=sigmoid function.

算法调优

算法偏差过大有两类问题:

  • Overfitting (过拟合 / High Variance)
  • Underfitting (欠拟合 / High Bias)
    针对这两个问题,我们要做的是:
    1. 识别他们
    2. 采取相应措施

如何产生正确的模型

首先将training samples分成三部分:

  • Training set: 60%
  • Cross validation set: 20%
  • Test set: 20%
    用training set来挑选,用Cross validation set来选择多项式幂次,最后用test set error 来评估算法的优劣。

Polynomial Degree - d

参考如下图像,

对于相同的,当多项式幂次很低时,可能会产生Underfitting,此时都很大
当多项式幂次很大时,可能会产生Overfitting,此时远大于,这对挑选d有很好的参考意义

Regularization -

  1. Create a list of lambdas (i.e. λ∈{0,0.01,0.02,0.04,…10.24});
  2. 计算不包含的train error和cross validation error
  3. 画出下图
  4. 和选择d类似,选取合适的

Random initialization

的初始值进行随机化。在ex4的练习中,给出了一下公式对进行初始化:


识别Overfitting(High Variance)/Underfitting(High Bias)

要识别,就要通过Learning Curves

High Bias:

High Variance:

What to try next?

  • Getting more training examples: Fixes high variance
  • Trying smaller sets of features: Fixes high variance
  • Adding features: Fixes high bias
  • Adding polynomial features: Fixes high bias
  • Decreasing λ: Fixes high bias
  • Increasing λ: Fixes high variance.

还有一个办法就是:
如果想得到好算法,做到两方面即可:

  1. 引入很多变量,参数 => high variance, low bias
  2. 提供大量training samples => low variance

Error Analysis

  • Start with a simple algorithm, implement it quickly, and test it early on your cross validation data.
  • Plot learning curves to decide if more data, more features, etc. are likely to help.
  • Manually examine the errors on examples in the cross validation set and try to spot a trend where most of the errors were made.

特例skewed data

当处理分类器时,如果有一个分类占绝对多数,例如99%。那可能我们对预测有偏向,比如要预测癌症,宁可误诊,不可漏诊,也就是要高Recall。

命中率

可以通过取不同的threshold,来做出下面这张图,根据Recall和Precision的偏好来选择需要的threshold。

如何评价算法优劣:
-Score (F Score) =

%23Machine%20Learning%20%282%29%20-%20Neural%20Network%0A%0A@%28myblog%29%5B%u6DF1%u5EA6%u5B66%u4E60%2C%20deep%20learning%5D%0A%0A%23%23Model%0A%21%5BAlt%20text%5D%28./1491893421634.png%29%0A%0A%24a_i%5E%7B%28j%29%7D%24%3A%20**activation**%20of%20unit%20i%20in%20layer%20j%0A%24%5CTheta%5E%7B%28j%29%7D%24%3A%20matrix%20of%20**weights**%20controlling%20function%20mapping%20from%20layer%20***j***%20to%20layer%20***j+1***%0A%0A%u8FD9%u662F%u4E00%u4E2A%u4E09%u5C42%u795E%u7ECF%u7F51%u7EDC%u3002%u6BCF%u4E00%u5C42%u90FD%u662F%u9075%u5FAALogistic%20Regression%u7684sigmoid%20function%uFF0C%u6240%u4EE5%u662F%u975E%u7EBF%u6027%u7684%u3002%20%0A%24a_1%5E%7B%282%29%7D%20%3D%20g%28%5CTheta_%7B10%7D%5E%7B%281%29%7D%20+%20%5CTheta_%7B11%7D%5E%7B%281%29%7Dx_1%20+%20%5CTheta_%7B12%7D%5E%7B%281%29%7Dx_2%20+%20%5CTheta_%7B13%7D%5E%7B%281%29%7Dx_3%29%20%24%0A%24a_2%5E%7B%282%29%7D%20%3D%20g%28%5CTheta_%7B20%7D%5E%7B%281%29%7D%20+%20%5CTheta_%7B21%7D%5E%7B%281%29%7Dx_1%20+%20%5CTheta_%7B22%7D%5E%7B%281%29%7Dx_2%20+%20%5CTheta_%7B13%7D%5E%7B%281%29%7Dx_3%29%20%24%0A%24a_3%5E%7B%282%29%7D%20%3D%20g%28%5CTheta_%7B30%7D%5E%7B%281%29%7D%20+%20%5CTheta_%7B31%7D%5E%7B%281%29%7Dx_1%20+%20%5CTheta_%7B32%7D%5E%7B%281%29%7Dx_2%20+%20%5CTheta_%7B13%7D%5E%7B%281%29%7Dx_3%29%20%24%0A%0AThe%20sigmoid%20function%20is%20%24g%28z%29%20%3D%20%5Cdfrac%20%7B1%7D%7B1+e%5E%7B-z%7D%7D%24%0A%0A%24%24h_%5CTheta%28x%29%20%3D%20a_1%5E%7B%283%29%7D%20%3D%20g%28%5CTheta_%7B10%7D%5E%7B%282%29%7Da_0%5E%7B%282%29%7D%20+%20%5CTheta_%7B11%7D%5E%7B%282%29%7Da_1%5E%7B2%7D%20+%20%5CTheta_%7B12%7D%5E%7B%282%29%7Da_2%5E%7B%282%29%7D%20+%20%5CTheta_%7B13%7D%5E%7B%282%29%7Da_3%5E%7B%282%29%7D%20%29%24%24%0A%0A%u901A%u8FC7hypothesis%20function%20%24h_%5Ctheta%28x%29%24%u5C31%u53EF%u4EE5%u8BA1%u7B97cost%20function%u3002%u516C%u5F0F%u5728%5BMachine%20Learning%20%281%29%20-%20Linear%20%26%20Logistic%20Regression%5D%28https%3A//app.yinxiang.com/shard/s10/nl/161681/b780cf32-b71b-44f8-929a-f91664ca0bc9%29%0A%u6458%u53D6%u516C%u5F0F%u5982%u4E0B%uFF1A%0A%24%24J%28%5Ctheta%29%20%3D%20-%5Cdfrac%20%7B1%7D%7Bm%7D%5Csum_%7Bi%3D1%7D%5Em%5By%5E%7B%28i%29%7Dlog%28h_%5Ctheta%28x%5E%7B%28i%29%7D%29%29%20+%20%281-y%5E%7B%28i%29%7D%29log%281-h_%5Ctheta%28x%5E%7B%28i%29%7D%29%29%5D%24%24%0A%0ANote%uFF1A%0A*%20%24%5CTheta%5E%7B%28j%29%7D%24%u7684%u5C3A%u5BF8%u4E3A%24s_%7Bj+1%7D%20%20%5Ctimes%20%28s_j+1%29%24%uFF0C%u53D6%u51B3%u4E8E%u4E0A%u4E00%u5C42%u548C%u8FD9%u4E00%u5C42%u7684node%u6570%u91CF%u3002%u5176%u4E2D%u7684+1%uFF0C%u662Fbias%u8282%u70B9%uFF0C%u5373%u5E38%u91CF%28+1%29%u8282%u70B9%uFF0C%u800C%u4E0D%u7B97%u5728%u8282%u70B9%u6570%u4E2D%0A*%20%24%5CTheta%24%u77E9%u9635%u7684%u6570%u91CF%u53D6%u51B3%u4E8E%u7F51%u7EDC%u5C42%u6570%uFF0C%u53733%u5C42%u7F51%u7EDC%uFF0C%u53EA%u67092%u4E2A%24%5CTheta%24%u77E9%u9635%0A%0A%23%23Cost%20Function%0A%u63A5%u7740%u4E0A%u4E00%u7AE0%u7684%u516C%u5F0F%uFF0C%u7ED9%u51FA%u5B8C%u6574%u7684%u591A%u72B6%u6001%u5206%u7C7B%u5E76%u4E14%u6B63%u89C4%u5316%u7684cost%20function%u5982%u4E0B%uFF1A%0A%24%24J%28%5CTheta%29%20%3D%20-%5Cdfrac%20%7B1%7D%7Bm%7D%20%5Csum_%7Bi%3D1%7D%5Em%20%5Csum_%7Bk%3D1%7D%5EK%5By_k%5E%7B%28i%29%7Dlog%28h_%5CTheta%28x%5E%7B%28i%29%7D%29%29_k%20+%20%281-y_k%5E%7B%28i%29%7D%29log%281-%28h_%5CTheta%28x%5E%7B%28i%29%7D%29%29_k%29%5D%20+%20%5Cdfrac%20%7B%5Clambda%7D%7B2m%7D%20%5Csum_%7Bl%3D1%7D%5E%7BL-1%7D%20%5Csum_%7Bi%3D1%7D%5E%7Bs_l%7D%20%5Csum_%7Bj%3D1%7D%5E%7Bs_l+1%7D%28%5CTheta_%7Bi%2Cj%7D%5E%7B%28l%29%7D%29%5E2%24%24%0A%u7A0D%u4F5C%u89E3%u91CA%uFF1A%0A*%20%u516C%u5F0F%u7684%u524D%u534A%u90E8%u5206%u662F%u539F%u7248%u7684hypothesis%uFF0C%u5E76%u5BF9%u6240%u6709%u7684%u5206%u7C7B%u8FDB%u884C%u7D2F%u52A0%0A*%20%u540E%u534A%u90E8%u5206%u662F%u6B63%u89C4%u5316%u53C2%u6570%uFF0C%u662F%u5BF9%u6240%u6709%u7684%24%5CTheta%24%u53C2%u6570%u8FDB%u884C%u5E73%u65B9%u7D2F%u52A0%u3002%u4E00%u5171%u6709L-1%u5C42%u795E%u7ECF%u7F51%u7EDC%uFF0C%u6BCF%u4E00%u5C42%u6709%24s_%7Bl+1%7D%5Ctimes%20s_l+1%24%u4E2A%24%5CTheta%24%0A%0A%3E**%u6CE8**%0A%3E%u6CE8%u610FRegularized%u9879%uFF0C%u53EA%u7D2F%u52A0%24s_%7Bj+1%7D%20%5Ctimes%20s_j%24%u9879%uFF0C%u4E0D%u5305%u542Bbias%u9879%24%5CTheta_%7Bi0%7D%24%0A%0A%23%23%23Backpropagation%20Algorithm%0A%u6709%u4E86cost%20function%uFF0C%u5C31%u53EF%u4EE5%u5F00%u59CBGradient%20descent%u4E86%uFF0C%u57FA%u672C%u516C%u5F0F%u5982%u4E0B%uFF1A%0A%24%24%5Ctheta_j%20%3A%3D%20%5Ctheta_j%20-%20%5Cdfrac%20%7B%5Calpha%7D%7Bm%7D%5Csum_%7Bi%3D1%7D%5Em%28h_%5Ctheta%28x%5E%7B%28i%29%7D%29-y%5E%7B%28i%29%7D%29%5Ccenterdot%20x_j%5E%7B%28i%29%7D%24%24%0A%u4F46%u662F%u73B0%u5728%u5F15%u5165%u4E86%u795E%u7ECF%u7F51%u7EDC%uFF0C%u6C42%u504F%u5BFC%u6570%u5C31%u6CA1%u8FD9%u4E48%u7B80%u5355%u4E86%uFF0C%u6240%u4EE5%u5F15%u5165%u4E86Forwardpropagation%20%26%20Backpropagation%u3002%u610F%u5373%uFF0C%u5411%u524D%u7B97%u4E00%u904D%uFF0C%u518D%u5411%u56DE%u7B97%u4E00%u904D%uFF0C%u7EFC%u5408%u4E24%u65B9%u9762%u7684%u6570%u636E%u5C31%u80FD%u5F97%u51FAcost%20function%u7684%u504F%u5BFC%u6570%u3002%u8BC1%u660E%u4E0D%u4F1A%uFF0C%u4F46%u662FAndrew%u7ED9%u51FA%u4E86%u516C%u5F0F%u3002%0A%u6574%u4E2A%u7B97%u6CD5%u8FC7%u7A0B%u5982%u4E0B%uFF0C%u8FD9%u91CC%u4E0D%u540C%u4E8E%u6559%u6750%u4E2D%u76844%u5C42%u795E%u7ECF%u7F51%u7EDC%uFF0C%u800C%u662F%u91C7%u7528%u672C%u7BC7%u7B14%u8BB0%u4E2D%u7684%u4E09%u5C42%u795E%u7ECF%u7F51%u7EDC%uFF1A%0A%3E%20Given%20training%20set%20%24%7B%28x%5E%7B%281%29%7D%2C%20y%5E%7B%281%29%7D%29%2C%20...%2C%20%28x%5E%7B%28m%29%7D%2C%20y%5E%7B%28m%29%7D%29%7D%24%0A%3E%20Set%20%24%5CDelta_%7Bij%7D%5E%7B%28l%29%7D%20%3D%200%20%24%20for%20all%20%28l%2Ci%2Cj%29%u3002%20size%28%24%5CDelta%24%29%20%3D%20%24s_%7Bj+1%7D%20%5Ctimes%20s_%7Bj%7D+1%24%uFF0C%u4E0E%24%5CTheta%24%u540C%0A%3E%20For%20i%20%3D1%20to%20m%0A%09%26nbsp%3B%26nbsp%3B%201.%20Set%20%24a%5E%7B%281%29%7D%20%3D%20x%5E%7B%28i%29%7D%24%2C%20both%20a%20and%20x%20are%20vectors%0A%09%26nbsp%3B%26nbsp%3B%202.%20Perform%20forward%20propagation%20to%20compute%20%24a%5E%7B%28l%29%7D%24%20for%20l%3D2%2C3%2C...%2CL.%20L%3D%u795E%u7ECF%u7F51%u7EDC%u5C42%u6570%0A%09%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%24a%5E%7B%281%29%7D%20%3D%20%5Cleft%5B%5Cbegin%7Bmatrix%7D%201%20%26%20x%20%5Cend%7Bmatrix%7D%20%5Cright%5D%24%20%u2014%u2014%u6CE8%u610F%u52A0%u5165bias%u9879%0A%09%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%24z%5E%7B%282%29%7D%20%3D%20%5CTheta%5E%7B%281%29%7Da%5E%7B%281%29%7D%24%0A%09%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%24a%5E%7B%282%29%7D%20%3D%20%5Cleft%5B%5Cbegin%7Bmatrix%7D%201%20%26%20g%28z%5E%7B%282%29%7D%29%5Cend%7Bmatrix%7D%20%5Cright%5D%24%20%u2014%u2014%u6CE8%u610F%u52A0%u5165bias%u9879%0A%09%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%24z%5E%7B%283%29%7D%20%3D%20%5CTheta%5E%7B%282%29%7Da%5E%7B%282%29%7D%24%0A%09%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%24a%5E%7B%283%29%7D%20%3D%20h_%5CTheta%28x%29%20%3D%20g%28z%5E%7B%283%29%7D%29%24%0A%09%3E%0A%20%20%20%26nbsp%3B%26nbsp%3B%203.%20Using%20%24y%5E%7B%28i%29%7D%24%2C%20compute%20%24%5Cdelta%5E%7B%28L%29%7D%20%3D%20a%5E%7B%28L%29%7D-%20y%20%5E%7B%28i%29%7D%24.%20y%u662F%u5BF9%u8F93%u51FA%u7684%u91CF%u6D4B%uFF0C%24a%5E%7B%28L%29%7D%20%3D%20h_%5CTheta%28x%29%24%u662F%u5BF9%u8F93%u51FA%u7684%u6A21%u578B%u4F30%u8BA1%uFF0C%24%5Cdelta%5E%7B%28L-1%29%7D%24%u7B97%u662F%u6A21%u578B%u504F%u5DEE%u3002%0A%20%20%20%26nbsp%3B%26nbsp%3B%204.%20Using%20backpropagation%20to%20compute%20%24%5Cdelta%5E%7B%28L-1%29%7D%2C%20%5Cdelta%5E%7B%28L-2%29%7D%2C%20...%2C%20%5Cdelta%5E%7B%282%29%7D%24%uFF0C%u8F93%u5165%u5C42%u6CA1%u6709%24%5Cdelta%24%uFF0C%u5373%24%5Cdelta%5E%7B%281%29%7D%24%0A%20%20%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%24%5Cdelta%5E%7B%28l%29%7D%3D%28%5CTheta%5E%7B%28l%29%7D%29%5ET%5Cdelta%5E%7B%28l+1%29%7D.*a%5E%7B%28l%29%7D.*%281-a%5E%7B%28l%29%7D%29%24%20%u2014%u2014%u6B64%u5904%u5E94%u5F53%u6709%u8BEF%uFF0C%u5177%u4F53%u53C2%u8003%24%5Cdelta%24%u76F8%u5173%u7B14%u8BB0%0A%20%20%0A%20%3E**%u6CE8%3A**%0A*%u9488%u5BF9%24%5Cdelta%24%u7684%u8BA1%u7B97%u8981%u7279%u522B%u6CE8%u610F%u3002%u4E0B%u9762%u4E13%u95E8%u653E%u4E00%u8282%u89E3%u91CA%24%5Cdelta%24%u8BA1%u7B97*%0A%20%20%20%20%0A%20%3E%26nbsp%3B%26nbsp%3B%205.%20Compute%20%24%5CDelta_%7Bij%7D%5E%7B%28l%29%7D%24%0A%20%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%24%5CDelta_%7Bij%7D%5E%7B%28l%29%7D%20%3A%3D%20%5CDelta_%7Bij%7D%5E%7B%28l%29%7D%20+%20a_j%5E%7B%28l%29%7D%5Cdelta_i%5E%7B%28l+1%29%7D%24%0A%20%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BVectorized%20equation%3A%20%24%5CDelta%5E%7B%28l%29%7D%20%3A%3D%20%5CDelta%5E%7B%28l%29%7D%20+%20%5Cdelta%5E%7B%28l+1%29%7D%28a%5E%7B%28l%29%7D%29%5ET%24%uFF0C%u5176%u4E2Dj%u662F%u6BCF%u4E00%u5C42%u7F51%u7EDC%u4E2D%u7684%u8282%u70B9%u7F16%u53F7%0A%20%3E**%u6CE8%3A**%0A%3E*%u6B64%u5904%u7D2F%u52A0%u6307%u7684%u662F%u9488%u5BF9training%20samples*%0A%0A%3E%20%26nbsp%3B%26nbsp%3B%206.%20Final%20step%2C%20compute%20the%20partial%20derivative%20of%20%24J%28%5CTheta%29%24%0A%20%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%24%5Cdfrac%20%7B%5Cpartial%7D%7B%5Cpartial%5CTheta_%7Bij%7D%5E%7B%28l%29%7D%7DJ%28%5CTheta%29%20%3D%20D_%7Bij%7D%5E%7B%28l%29%7D%24%0A%20%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%24D_%7Bij%7D%5E%7B%28l%29%7D%20%3A%3D%20%5Cdfrac%20%7B1%7D%7Bm%7D%20%5CDelta_%7Bij%7D%5E%7B%28l%29%7D%20+%20%5Clambda%20%5CTheta_%7Bij%7D%5E%7B%28l%29%7D%20%5Chspace%7B1em%7D%20if%20%5Chspace%7B0.1em%7D%20j%20%5Cneq%200%24%20%20%0A%20%20%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%24D_%7Bij%7D%5E%7B%28l%29%7D%20%3A%3D%20%5Cdfrac%20%7B1%7D%7Bm%7D%20%5CDelta_%7Bij%7D%5E%7B%28l%29%7D%20%5Chspace%7B4em%7D%20if%20%5Chspace%7B0.1em%7D%20j%20%3D%200%24%0A%0A%u6574%u4E2A%u8FC7%u7A0B%u7565%u663E%u590D%u6742%uFF0C%u4F46%u662F%u8FD9%u5C31%u662F%u795E%u7ECF%u7F51%u7EDC%u3002%u636E%u8BF4%u540E%u9762%u6709%u66F4%u4F18%u96C5%u7684%u7B97%u6CD5%u3002%0A%0A%23%23%23%u8BA1%u7B97%24%5Cdelta%24%0A%3E%24%5Cdelta%24%3A%20an%20error%20item%20that%20measures%20how%20much%20the%20node%20was%20responsible%20for%20any%20errors%20in%20our%20output.%0A%0A%24cost%28t%29%20%3D%20y%5E%7B%28t%29%7Dlog%28h_%5CTheta%28x%5E%7B%28t%29%7D%29%29%20+%20%281-y%5E%7B%28t%29%7D%29log%281-h_%5CTheta%28x%5E%7B%28t%29%7D%29%29%24%0A%24%5Cdelta_j%5E%7B%28t%29%7D%20%3D%20%5Cdfrac%20%7B%5Cpartial%7D%7B%5Cpartial%20z_j%5E%7B%28l%29%7D%7Dcost%28t%29%20%3D%20%28%5CTheta%5E%7B%28l%29%7D%29%5ET%5Cdelta%5E%7B%28l+1%29%7D.*g%27%28z%29%20%24%0A%24g%27%28z%29%20%3D%20%28%5Cdfrac%20%7B1%7D%7B1+e%5E%7B-z%7D%7D%29%27%20%3D%20g%28z%29%281-g%28z%29%29%24%0A%0A%u6700%u7EC8%u8BA1%u7B97%u5F0F%u5E94%u4E3A%uFF1A%0A%24%24%5Cdelta_j%5E%7B%28t%29%7D%20%3D%20%28%5CTheta%5E%7B%28l%29%7D%29%5ET%5Cdelta%5E%7B%28l+1%29%7D.*g%28%5CTheta%5E%7B%28l-1%29%7D%20a%5E%7B%28l-1%29%7D%29%20.*%281-g%28%5CTheta%5E%7B%28l-1%29%7Da%5E%7B%28l-1%29%7D%29%29%24%24%0A%0Ag%3Dsigmoid%20function.%0A%20%20%0A%23%23%u7B97%u6CD5%u8C03%u4F18%0A%u7B97%u6CD5%u504F%u5DEE%u8FC7%u5927%u6709%u4E24%u7C7B%u95EE%u9898%uFF1A%0A-%20Overfitting%20%28%u8FC7%u62DF%u5408%20/%20High%20Variance%29%0A-%20Underfitting%20%28%u6B20%u62DF%u5408%20/%20High%20Bias%29%0A%u9488%u5BF9%u8FD9%u4E24%u4E2A%u95EE%u9898%uFF0C%u6211%u4EEC%u8981%u505A%u7684%u662F%uFF1A%0A1.%20%u8BC6%u522B%u4ED6%u4EEC%0A2.%20%u91C7%u53D6%u76F8%u5E94%u63AA%u65BD%0A%0A%23%23%23%u5982%u4F55%u4EA7%u751F%u6B63%u786E%u7684%u6A21%u578B%0A%u9996%u5148%u5C06training%20samples%u5206%u6210%u4E09%u90E8%u5206%uFF1A%0A-%20Training%20set%3A%2060%25%0A-%20Cross%20validation%20set%3A%2020%25%0A-%20Test%20set%3A%2020%25%0A%u7528training%20set%u6765%u6311%u9009%24%5CTheta%2C%20%5Clambda%24%uFF0C%u7528Cross%20validation%20set%u6765%u9009%u62E9%u591A%u9879%u5F0F%u5E42%u6B21%uFF0C%u6700%u540E%u7528test%20set%20error%20%24J_%7Btest%7D%28%5CTheta%5E%7B%28d%29%7D%29%24%u6765%u8BC4%u4F30%u7B97%u6CD5%u7684%u4F18%u52A3%u3002%0A%21%5BAlt%20text%7C400x0%5D%28./1502938070114.png%29%0A%0A%0A%23%23%23%23Polynomial%20Degree%20-%20d%0A%u53C2%u8003%u5982%u4E0B%u56FE%u50CF%uFF0C%0A%21%5BAlt%20text%5D%28./1493951705755.png%29%0A%0A%3E%u5BF9%u4E8E%u76F8%u540C%u7684%24%5CTheta%24%uFF0C%u5F53%u591A%u9879%u5F0F%u5E42%u6B21%u5F88%u4F4E%u65F6%uFF0C%u53EF%u80FD%u4F1A%u4EA7%u751FUnderfitting%uFF0C%u6B64%u65F6%24J_cv%24%u548C%24J_training%24%u90FD%u5F88%u5927%0A%u5F53%u591A%u9879%u5F0F%u5E42%u6B21%u5F88%u5927%u65F6%uFF0C%u53EF%u80FD%u4F1A%u4EA7%u751FOverfitting%uFF0C%u6B64%u65F6%24J_cv%24%u8FDC%u5927%u4E8E%24J_training%24%uFF0C%u8FD9%u5BF9%u6311%u9009d%u6709%u5F88%u597D%u7684%u53C2%u8003%u610F%u4E49%0A%0A%23%23%23%23Regularization%20%20-%20%24%5Clambda%24%0A1.%20Create%20a%20list%20of%20lambdas%20%28i.e.%20%u03BB%u2208%7B0%2C0.01%2C0.02%2C0.04%2C...10.24%7D%29%3B%0A2.%20%u8BA1%u7B97%u4E0D%u5305%u542B%24%5Clambda%24%u7684train%20error%u548Ccross%20validation%20error%0A3.%20%u753B%u51FA%u4E0B%u56FE%0A4.%20%u548C%u9009%u62E9d%u7C7B%u4F3C%uFF0C%u9009%u53D6%u5408%u9002%u7684%24%5Clambda%24%0A%0A%21%5BAlt%20text%7C350x0%5D%28./1493952692430.png%29%0A%0A%23%23%23%23Random%20initialization%0A%u5BF9%24%5CTheta%24%u7684%u521D%u59CB%u503C%u8FDB%u884C%u968F%u673A%u5316%u3002%u5728ex4%u7684%u7EC3%u4E60%u4E2D%uFF0C%u7ED9%u51FA%u4E86%u4E00%u4E0B%u516C%u5F0F%u5BF9%24%5CTheta%24%u8FDB%u884C%u521D%u59CB%u5316%uFF1A%0A%24%24%5Cepsilon_%7Binit%7D%20%3D%20%5Cdfrac%7B%5Csqrt%7B6%7D%7D%7B%5Csqrt%20%7BL%5C_in%20+L%5C_out%7D%7D%24%24%0A%24L%5C_in%20%3D%20s_l%24%0A%24L%5C_out%20%3D%20s_%7Bl+1%7D%24%0A%0A%23%23%23%u8BC6%u522BOverfitting%28High%20Variance%29/Underfitting%28High%20Bias%29%0A%u8981%u8BC6%u522B%uFF0C%u5C31%u8981%u901A%u8FC7Learning%20Curves%0A%0A**High%20Bias%3A**%0A%21%5BAlt%20text%7C350x0%5D%28./1493950678949.png%29%0ATest%20error%u548Ctrain%20error%u63A5%u8FD1%uFF0C%u800C%u4E14%u90FD%u5F88%u9AD8%0A%0A**High%20Variance%3A**%0A%21%5BAlt%20text%7C350x0%5D%28./1493952926446.png%29%0ATrain%20error%u63A5%u8FD1%u6B63%u786E%u503C%uFF0C%u4F46%u662Ftest%20error%u8FD8%u662F%u5F88%u9AD8%uFF0C%u968F%u7740training%20set%20size%u53D8%u5927%uFF0Ctest%20error%u4E5F%u5728%u51CF%u5C0F%uFF0C%u4F46%u662F%u51CF%u5C0F%u7684%u901F%u5EA6%u5F88%u6162%u3002%0A%0A%23%23%23What%20to%20try%20next%3F%0A%3E%20-%20Getting%20more%20training%20examples%3A%20Fixes%20high%20variance%0A%3E-%20Trying%20smaller%20sets%20of%20features%3A%20Fixes%20high%20variance%0A%3E-%20Adding%20features%3A%20Fixes%20high%20bias%0A%3E-%20Adding%20polynomial%20features%3A%20Fixes%20high%20bias%0A%3E-%20Decreasing%20%u03BB%3A%20Fixes%20high%20bias%0A%3E-%20Increasing%20%u03BB%3A%20Fixes%20high%20variance.%0A%0A%u8FD8%u6709%u4E00%u4E2A%u529E%u6CD5%u5C31%u662F%uFF1A%0A%u5982%u679C%u60F3%u5F97%u5230%u597D%u7B97%u6CD5%uFF0C%u505A%u5230%u4E24%u65B9%u9762%u5373%u53EF%uFF1A%0A1.%20%u5F15%u5165%u5F88%u591A%u53D8%u91CF%uFF0C%u53C2%u6570%20%3D%3E%20high%20variance%2C%20low%20bias%0A2.%20%u63D0%u4F9B%u5927%u91CFtraining%20samples%20%3D%3E%20low%20variance%0A%23%23%23Error%20Analysis%0A%3E-%20Start%20with%20a%20simple%20algorithm%2C%20implement%20it%20quickly%2C%20and%20test%20it%20early%20on%20your%20cross%20validation%20data.%0A%3E-%20Plot%20learning%20curves%20to%20decide%20if%20more%20data%2C%20more%20features%2C%20etc.%20are%20likely%20to%20help.%0A%3E-%20Manually%20examine%20the%20errors%20on%20examples%20in%20the%20cross%20validation%20set%20and%20try%20to%20spot%20a%20trend%20where%20most%20of%20the%20errors%20were%20made.%0A%0A%23%23%23%u7279%u4F8Bskewed%20data%0A%u5F53%u5904%u7406%u5206%u7C7B%u5668%u65F6%uFF0C%u5982%u679C%u6709%u4E00%u4E2A%u5206%u7C7B%u5360%u7EDD%u5BF9%u591A%u6570%uFF0C%u4F8B%u598299%25%u3002%u90A3%u53EF%u80FD%u6211%u4EEC%u5BF9%u9884%u6D4B%u6709%u504F%u5411%uFF0C%u6BD4%u5982%u8981%u9884%u6D4B%u764C%u75C7%uFF0C%u5B81%u53EF%u8BEF%u8BCA%uFF0C%u4E0D%u53EF%u6F0F%u8BCA%uFF0C%u4E5F%u5C31%u662F%u8981%u9AD8Recall%u3002%0A%21%5BAlt%20text%5D%28./1493953864306.png%29%0A%24Precision%20%3D%20%5Cdfrac%20%7Btrue%5C%20positive%7D%7Btrue%5C%20positive%20+%20false%5C%20positive%7D%24%20%u9884%u6D4B%u547D%u4E2D%u7684%u51C6%u786E%u6027%0A%0A%24Recall%20%3D%20%5Cdfrac%20%7Btrue%5C%20positive%7D%7Btrue%5C%20positive%20+%20false%5C%20negative%7D%24%20%u547D%u4E2D%u7387%0A%0A%24Accuracy%20%3D%20%5Cdfrac%20%7Btrue%5C%20positive%20+%20true%5C%20negative%7D%20%7Boverall%5C%20samples%7D%24%0A%0A%u53EF%u4EE5%u901A%u8FC7%u53D6%u4E0D%u540C%u7684threshold%uFF0C%u6765%u505A%u51FA%u4E0B%u9762%u8FD9%u5F20%u56FE%uFF0C%u6839%u636ERecall%u548CPrecision%u7684%u504F%u597D%u6765%u9009%u62E9%u9700%u8981%u7684threshold%u3002%0A%21%5BAlt%20text%5D%28./1493961624396.png%29%0A%0A**%u5982%u4F55%u8BC4%u4EF7%u7B97%u6CD5%u4F18%u52A3%uFF1A**%0A%24F1%24-Score%20%28F%20Score%29%20%3D%20%242%5Cdfrac%20%7BPR%7D%7BP+R%7D%24%0A%0A%0A

Edit

Linear Regression

Linear Regression = 线性回归。

Single Feature Hypothesis:

通过求cost function的最小值,来估算
列出单feature的cost function,不做多介绍。重点放在Multiple Feature Linear Regression。

Cost Function:

Cost Function & Gradient Descent

Multiple Feature Hypothesis:

Multiple Feature Cost Function:

Gradient Descent:

  • j := 0…m
  • - Learning rate.

How to choose learning rate - ?

  • If is too small: slow convergence
  • If is too large: J() may not decrease on every iteration; may not converge.

To choose , try:
…, 0.001, 0.003, 0.01, 0.03, 0.1, 0.3, 1, …
每次取3倍。

Feature Scaling

We can speed up gradient descent by having each of our input values in roughly the same range. This is because θ will descend quickly on small ranges and slowly on large ranges, and so will oscillate inefficiently down to the optimum when the variables are very uneven.

用下面的公式预处理我们的training set,就是Feature Scaling

  • is the average of all the values for feature (i)
  • is the range of values (max - min)
  • or is the standard deviation.

* standard deviation (标准差)=

Normal Equation正规解

  • 注1:training set的数量要大于feature数量,否则会不可逆,导致没有解
  • 注2:正规解不需要feature scaling

Logistic Regression

Hypothesis:

Sigmoid Function / Logistic Function

The following image shows us what the sigmoid function looks like:

这里的Hypothesis方程是不平滑的,会导致很多的local optimization,导致Gradient Descent不能成功收敛。

用条件概率表示Hypothesis:
y=1的条件下,x,的取值概率。

Cost Function



这其中用Cost函数取代了线性回归中的

将Cost函数代入整个Cost Function中可得:

Vectorized implementation:

进一步计算Gradient Descent的迭代算法为:

矢量化写法:

Overfitting过拟合

Regularized Linear Regression

The λ, or lambda, is the regularization parameter. It determines how much the costs of our theta parameters are inflated.

Regularized Logistic Regression

The second sum, means to explicitly exclude the bias term

!!注意:这里的regularized项,不包含,如果在Matlab/Octave中就是

%23Machine%20Learning%20%281%29%20-%20Linear%20%26%20Logistic%20Regression%0A%0A@%28%u5B66%u4E60%u7B14%u8BB0%29%0A%0A%0A%5BTOC%5D%0A%0A%21%5BAlt%20text%5D%28./1491028424509.png%29%0A%0A%23%23Linear%20Regression%0ALinear%20Regression%20%3D%20%u7EBF%u6027%u56DE%u5F52%u3002%0A%0ASingle%20Feature%20**Hypothesis**%3A%0A%24%24h_%7B%5Ctheta%7D%28x%29%20%20%3D%20%5Ctheta_%7B0%7D%20+%20%5Ctheta_%7B1%7Dx%24%24%0A%u901A%u8FC7%u6C42cost%20function%u7684%u6700%u5C0F%u503C%uFF0C%u6765%u4F30%u7B97%24%5Ctheta_%7Bi%7D%24%u3002%0A%u5217%u51FA%u5355feature%u7684cost%20function%uFF0C%u4E0D%u505A%u591A%u4ECB%u7ECD%u3002%u91CD%u70B9%u653E%u5728Multiple%20Feature%20Linear%20Regression%u3002%0A%0A**Cost%20Function**%3A%0A%24%24J%28%5Ctheta%29%20%3D%20%5Cdfrac%20%7B1%7D%7B2m%7D%20%5Csum_%7Bi%3D1%7D%5Em%20%28h_%5Ctheta%28x_%7Bi%7D%29%20-%20y_%7Bi%7D%29%5E2%24%24%0A%0A%23%23%23Cost%20Function%20%26%20Gradient%20Descent%0AMultiple%20Feature%20**Hypothesis**%3A%0A%24%24h_%5Ctheta%28x%29%20%3D%20%5Cbegin%20%7Bbmatrix%7D%5Ctheta_0%20%5Chspace%7B1em%7D%20%5Ctheta_1%20%5Chspace%7B1em%7D...%20%5Chspace%7B1em%7D%5Ctheta_n%20%5Cend%7Bbmatrix%7D%20%5Cbegin%7Bbmatrix%7Dx_0%20%5Cnewline%20x_1%20%5Cnewline%20%5Cvdots%20%5Cnewline%20x_n%5Cend%7Bbmatrix%7D%20%3D%20%5Ctheta%5ETx%24%24%0A%0AMultiple%20Feature%20**Cost%20Function**%3A%0A%24%24J%28%5Ctheta%29%20%3D%20%5Cdfrac%20%7B1%7D%7B2m%7D%5Csum_%7Bi%3D1%7D%5Em%28h_%5Ctheta%28x%5E%7B%28i%29%7D%29-y%5E%7B%28i%29%7D%29%5E2%24%24%0A%0AGradient%20Descent%3A%0A%24%24%5Ctheta_j%20%3A%3D%20%5Ctheta_j%20-%20%5Calpha%5Cdfrac%20%7B%5Cpartial%7D%7B%5Cpartial%5Ctheta_j%7D%20J%28%5Ctheta%29%20%3D%20%5Ctheta_j%20-%20%5Calpha%5Cdfrac%20%7B1%7D%7Bm%7D%5Csum_%7Bi%3D1%7D%5Em%28h_%5Ctheta%28x%5E%7B%28i%29%7D%29%20-%20y%5E%7B%28i%29%7D%29%5Ccenterdot%20x_j%5E%7B%28i%29%7D%24%24%20%0A-%20j%20%3A%3D%200...m%0A-%20%24%5Calpha%24%20-%20Learning%20rate.%0A%0A%23%23%23How%20to%20choose%20learning%20rate%20-%20%24%5Calpha%24%20%3F%0A-%20If%20%24%5Calpha%24%20is%20too%20small%3A%20slow%20convergence%0A-%20If%20%24%5Calpha%24%20is%20too%20large%3A%20J%28%24%5Ctheta%24%29%20may%20not%20decrease%20on%20every%20iteration%3B%20may%20not%20converge.%0A%0ATo%20choose%20%24%5Calpha%24%2C%20try%3A%0A%09...%2C%200.001%2C%200.003%2C%200.01%2C%200.03%2C%200.1%2C%200.3%2C%201%2C%20...%0A%u6BCF%u6B21%u53D63%u500D%u3002%0A%0A%23%23%23Feature%20Scaling%0A%3EWe%20can%20speed%20up%20gradient%20descent%20by%20having%20each%20of%20our%20input%20values%20in%20**roughly%20the%20same%20range**.%20This%20is%20because%20%u03B8%20will%20descend%20quickly%20on%20small%20ranges%20and%20slowly%20on%20large%20ranges%2C%20and%20so%20will%20oscillate%20inefficiently%20down%20to%20the%20optimum%20when%20the%20variables%20are%20very%20uneven.%0A%0A%u7528%u4E0B%u9762%u7684%u516C%u5F0F%u9884%u5904%u7406%u6211%u4EEC%u7684training%20set%uFF0C%u5C31%u662FFeature%20Scaling%0A%24%24x_i%20%3A%3D%20%5Cdfrac%7Bx_i%20-%20%5Cmu_i%7D%7Bs_i%7D%20%24%24%0A-%20%24%5Cmu_i%24%20is%20the%20average%20of%20all%20the%20values%20for%20feature%20%28i%29%0A-%20%24s_i%24%20is%20the%20range%20of%20values%20%28max%20-%20min%29%0A-%20**or**%20%24s_i%24%20is%20the%20standard%20deviation.%0A%0A%5C*%20*standard%20deviation%20%28%u6807%u51C6%u5DEE%29%3D%20%24%5Csqrt%7B%5Cdfrac%7B1%7D%7BN%7D%5Csum_%7Bi%3D1%7D%5EN%28x_i%20-%20%5Cmu%292%7D%24*%0A%0A%23%23%23Normal%20Equation%u6B63%u89C4%u89E3%0A%24%24%5Ctheta%20%3D%20%28X%5ETX%29%5E%7B-1%7DX%5ETy%24%24%0A*%20%u6CE81%uFF1Atraining%20set%u7684%u6570%u91CF%u8981%u5927%u4E8Efeature%u6570%u91CF%uFF0C%u5426%u5219%24X%5ETX%24%u4F1A%u4E0D%u53EF%u9006%uFF0C%u5BFC%u81F4%u6CA1%u6709%u89E3%0A*%20%u6CE82%uFF1A%u6B63%u89C4%u89E3%u4E0D%u9700%u8981feature%20scaling%0A%0A%23%23Logistic%20Regression%0A**Hypothesis**%3A%0A%24%24h_%5Ctheta%28x%29%20%3D%20g%28%5Ctheta%5ETx%29%24%24%0A**Sigmoid%20Function%20/%20Logistic%20Function**%0A%24%24g%28z%29%20%3D%20%5Cdfrac%20%7B1%7D%7B1+e%5E%7B-z%7D%7D%24%24%0A%24z%20%3D%20%5Ctheta%5ETx%24%0A%0AThe%20following%20image%20shows%20us%20what%20the%20sigmoid%20function%20looks%20like%3A%0A%21%5BAlt%20text%5D%28./1491033397652.png%29%0A%0A%u8FD9%u91CC%u7684Hypothesis%u65B9%u7A0B%u662F%u4E0D%u5E73%u6ED1%u7684%uFF0C%u4F1A%u5BFC%u81F4%u5F88%u591A%u7684local%20optimization%uFF0C%u5BFC%u81F4Gradient%20Descent%u4E0D%u80FD%u6210%u529F%u6536%u655B%u3002%0A%0A%u7528%u6761%u4EF6%u6982%u7387%u8868%u793AHypothesis%3A%0Ay%3D1%u7684%u6761%u4EF6%u4E0B%uFF0Cx%2C%24%5Ctheta%24%u7684%u53D6%u503C%u6982%u7387%u3002%0A%24%24h_%5Ctheta%28x%29%3DP%28y%3D1%5Cmid%20x%3B%u03B8%29%3D1%u2212P%28y%3D0%20%5Cmid%20x%3B%u03B8%29%24%24%0A%24%24P%28y%3D0%20%5Cmid%20x%3B%u03B8%29+P%28y%3D1%20%5Cmid%20x%3B%u03B8%29%3D1%24%24%0A%0A%0A%0A%23%23%23%20Cost%20Function%0A%24%24J%28%5Ctheta%29%20%3D%20-%5Cdfrac%20%7B1%7D%7Bm%7D%20%5Csum_%7Bi%3D1%7D%5EmCost%28h_%5Ctheta%28x%5E%7B%28i%29%7D%29%2C%20y%5E%7B%28i%29%7D%29%24%24%0A%24%24Cost%28h_%5Ctheta%28x%29%2C%20y%29%29%20%3D%20-log%28h_%5Ctheta%28x%29%29%20%20%5Chspace%7B10em%7Dif%20y%20%3D%201%24%24%0A%24%24Cost%28h_%5Ctheta%28x%29%2C%20y%29%29%20%3D%20-log%281-h_%5Ctheta%28x%29%29%20%5Chspace%7B8em%7D%20if%20y%20%3D%200%24%24%0A%0A%24Cost%28h_%5Ctheta%28x%29%2C%20y%29%29%20%3D%200%20%5Chspace%7B3em%7D%20if%20%5Chspace%7B1em%7D%20h_%5Ctheta%28x%29%20%3D%20y%24%0A%24Cost%28h_%5Ctheta%28x%29%2C%20y%29%29%20%5Crightarrow%20%5Cinfty%20%5Chspace%7B2em%7D%20if%20y%3D0%20%5Chspace%7B0.5em%7D%20and%20%5Chspace%7B0.5em%7D%20h_%5Ctheta%28x%29%20%5Crightarrow%201%24%0A%24Cost%28h_%5Ctheta%28x%29%2C%20y%29%29%20%5Crightarrow%20%5Cinfty%20%5Chspace%7B2em%7D%20if%20y%3D1%20%5Chspace%7B0.5em%7D%20and%20%5Chspace%7B0.5em%7D%20h_%5Ctheta%28x%29%20%5Crightarrow%200%24%0A%0A%u8FD9%u5176%u4E2D%u7528Cost%u51FD%u6570%u53D6%u4EE3%u4E86%u7EBF%u6027%u56DE%u5F52%u4E2D%u7684%24%5Cdfrac%20%7B1%7D%7B2%7D%28h_%5Ctheta%28x_i%29-y_i%29%5E2%24%0A%0A%u5C06Cost%u51FD%u6570%u4EE3%u5165%u6574%u4E2ACost%20Function%u4E2D%u53EF%u5F97%uFF1A%0A%24J%28%5Ctheta%29%20%3D%20-%5Cdfrac%20%7B1%7D%7Bm%7D%5Csum_%7Bi%3D1%7D%5Em%5By%5E%7B%28i%29%7Dlog%28h_%5Ctheta%28x%5E%7B%28i%29%7D%29%29%20+%20%281-y%5E%7B%28i%29%7D%29log%281-h_%5Ctheta%28x%5E%7B%28i%29%7D%29%29%5D%24%0A%0A**Vectorized%20implementation**%3A%0A%24h%3Dg%28X%5Ctheta%29%20%3D%20%5Cdfrac%20%7B1%7D%7B1+e%5E%7B-%5Ctheta%5ETx%7D%7D%24%0A%24J%28%5Ctheta%29%20%3D%20%5Cdfrac%20%7B1%7D%7Bm%7D%5Ccenterdot%20%28-y%5ETlog%28h_%5Ctheta%29-%281-y%29%5ETlog%281-h_%5Ctheta%29%29%24%0A%0A%u8FDB%u4E00%u6B65%u8BA1%u7B97Gradient%20Descent%u7684%u8FED%u4EE3%u7B97%u6CD5%u4E3A%uFF1A%0A%24%24%5Ctheta_j%20%3A%3D%20%5Ctheta_j%20-%20%5Cdfrac%20%7B%5Calpha%7D%7Bm%7D%5Csum_%7Bi%3D1%7D%5Em%28h_%5Ctheta%28x%5E%7B%28i%29%7D%29-y%5E%7B%28i%29%7D%29%5Ccenterdot%20x_j%5E%7B%28i%29%7D%24%24%0A%0A%u77E2%u91CF%u5316%u5199%u6CD5%uFF1A%0A%24%24%5Ctheta%20%3A%3D%20%5Ctheta%20-%20%5Cdfrac%7B%5Calpha%7D%7Bm%7DX%5ET%28g%28X%5Ctheta%29%20-%20%5Cvec%7By%7D%29%24%24%0A%0A%23%23Overfitting%u8FC7%u62DF%u5408%0A%23%23%23Regularized%20Linear%20Regression%0A%24%24J%28%5Ctheta%29%20%3D%20%5Cdfrac%7B1%7D%7B2m%7D%5Csum_%7Bi%3D1%7D%5Em%28h_%5Ctheta%28x%5E%7B%28i%29%7D%29-y%5E%7Bi%7D%29%5E2+%5Clambda%5Csum_%7Bj%3D1%7D%5En%5Ctheta_j%5E2%24%24%0A%3EThe%20%u03BB%2C%20or%20lambda%2C%20is%20the%20regularization%20parameter.%20It%20determines%20how%20much%20the%20costs%20of%20our%20theta%20parameters%20are%20inflated.%0A%23%23%23Regularized%20Logistic%20Regression%0A%24%24J%28%5Ctheta%29%20%3D%20-%5Cdfrac%20%7B1%7D%7Bm%7D%5Csum_%7Bi%3D1%7D%5Em%5By%5E%7B%28i%29%7Dlog%28h_%5Ctheta%28x%5E%7B%28i%29%7D%29%29%20+%20%281-y%5E%7B%28i%29%7D%29log%281-h_%5Ctheta%28x%5E%7B%28i%29%7D%29%29%5D%20+%20%5Cdfrac%20%7B%5Clambda%7D%7B2m%7D%5Csum_%7Bj%3D1%7D%5En%5Ctheta_j%5E2%24%24%0AThe%20second%20sum%2C%24%5Csum_%7Bj%3D1%7D%5En%5Ctheta_j%5E2%24%20means%20to%20**explicitly%20exclude%20the%20bias%20term**%20%24%5Ctheta_0%24%0A%0A%21%21%u6CE8%u610F%uFF1A%u8FD9%u91CC%u7684regularized%u9879%uFF0C%u4E0D%u5305%u542B%24%5Ctheta_0%24%uFF0C%u5982%u679C%u5728Matlab/Octave%u4E2D%u5C31%u662F%24%5Ctheta_1%24%u3002