| 21 | return X1.dot(X2.T) + c |
| 22 | |
| 23 | def rbf(X1, X2, gamma=None): |
| 24 | if gamma is None: |
| 25 | gamma = 1.0 / X1.shape[-1] # 1 / D |
| 26 | # gamma = 0.05 |
| 27 | # gamma = 5. # for donut and spiral |
| 28 | if np.ndim(X1) == 1 and np.ndim(X2) == 1: |
| 29 | result = np.exp(-gamma * np.linalg.norm(X1 - X2)**2) |
| 30 | elif (np.ndim(X1) > 1 and np.ndim(X2) == 1) or (np.ndim(X1) == 1 and np.ndim(X2) > 1): |
| 31 | result = np.exp(-gamma * np.linalg.norm(X1 - X2, axis=1)**2) |
| 32 | elif np.ndim(X1) > 1 and np.ndim(X2) > 1: |
| 33 | result = np.exp(-gamma * np.linalg.norm(X1[:, np.newaxis] - X2[np.newaxis, :], axis=2)**2) |
| 34 | return result |
| 35 | |
| 36 | def sigmoid(X1, X2, gamma=0.05, c=1): |
| 37 | return np.tanh(gamma * X1.dot(X2.T) + c) |