markdown
stringlengths
0
1.02M
code
stringlengths
0
832k
output
stringlengths
0
1.02M
license
stringlengths
3
36
path
stringlengths
6
265
repo_name
stringlengths
6
127
数据清洗大多机器学习算法不能处理缺失的特征,因此先创建一些函数来处理特征缺失的问题。前面,你应该注意到了属性 total_bedrooms 有一些缺失值。有三个解决选项:* 去掉对应的街区;* 去掉整个属性;* 进行赋值(0、平均值、中位数等等)。用 DataFrame 的 `dropna()`,`drop()`,和 `fillna()` 方法,可以方便地实现:```pythonhousing.dropna(subset=["total_bedrooms"]) 选项1housing.drop("total_bedrooms", axis= 1) 选项2median = housing["total_bedr...
from sklearn.impute import SimpleImputer imputer = SimpleImputer(missing_values=np.nan, strategy='mean') housing_num = housing.drop("ocean_proximity", axis=1) imputer.fit(housing_num) X = imputer.transform(housing_num) housing_tr = pd.DataFrame(X, columns=housing_num.columns) housing_tr.info()
<class 'pandas.core.frame.DataFrame'> RangeIndex: 16512 entries, 0 to 16511 Data columns (total 8 columns): longitude 16512 non-null float64 latitude 16512 non-null float64 housing_median_age 16512 non-null float64 total_rooms 16512 non-null float64 total_bedrooms 16512 non-...
MIT
sklearn-guide/chapter03/ml-3.ipynb
a630140621/machine-learning-course
处理文本和类别属性前面,我们丢弃了类别属性 ocean_proximity,因为它是一个文本属性,不能计算出中位数。__大多数机器学习算法跟喜欢和数字打交道,所以让我们把这些文本标签转换为数字__。 LabelEncoderScikit-Learn 为这个任务提供了一个转换器 `LabelEncoder`
from sklearn.preprocessing import LabelEncoder encoder = LabelEncoder() housing_cat = housing["ocean_proximity"] housing_cat_encoded = encoder.fit_transform(housing_cat) housing_cat_encoded encoder.classes_ # <1H OCEAN 被映射为 0, INLAND 被映射为 1 等等
_____no_output_____
MIT
sklearn-guide/chapter03/ml-3.ipynb
a630140621/machine-learning-course
OneHotEncoder注意输出结果是一个 SciPy 稀疏矩阵,而不是 NumPy 数组。> 当类别属性有数千个分类时,这样非常有用。经过独热编码,我们得到了一个有数千列的矩阵,这个矩阵每行只有一个 1,其余都是 0。使用大量内存来存储这些 0 非常浪费,所以稀疏矩阵只存储非零元素的位置。你可以像一个 2D 数据那样进行使用,但是如果你真的想将其转变成一个(密集的)NumPy 数组,只需调用 `toarray()` 方法。
from sklearn.preprocessing import OneHotEncoder encoder = OneHotEncoder() housing_cat_1hot = encoder.fit_transform(housing_cat_encoded.reshape( -1 , 1 )) housing_cat_1hot housing_cat_1hot.toarray()
_____no_output_____
MIT
sklearn-guide/chapter03/ml-3.ipynb
a630140621/machine-learning-course
LabelBinarizer使用类 LabelBinarizer ,我们可以用一步执行这两个转换。> 向构造器 `LabelBinarizer` 传递 `sparse_output=True`,就可以得到一个稀疏矩阵。
from sklearn.preprocessing import LabelBinarizer encoder = LabelBinarizer() housing_cat_1hot = encoder.fit_transform(housing_cat) housing_cat_1hot
_____no_output_____
MIT
sklearn-guide/chapter03/ml-3.ipynb
a630140621/machine-learning-course
自定义转换器尽管 Scikit-Learn 提供了许多有用的转换器,你还是需要自己动手写转换器执行任务,比如自定义的清理操作,或属性组合。你需要让自制的转换器与 Scikit-Learn 组件(比如流水线)无缝衔接工作,因为 Scikit-Learn 是依赖鸭子类型的(而不是继承),你所需要做的是创建一个类并执行三个方法: `fit()`(返回 `self` ),`transform()` ,和 `fit_transform()`。通过添加 `TransformerMixin` 作为基类,可以很容易地得到最后一个。另外,如果你添加 `BaseEstimator` 作为基类(且构造器中避免使用 `*args` 和 `**kargs`...
from sklearn.base import BaseEstimator, TransformerMixin rooms_ix, bedrooms_ix, population_ix, household_ix = 3, 4, 5, 6 class CombinedAttributesAdder(BaseEstimator, TransformerMixin): def __init__ (self, add_bedrooms_per_room = True): # no *args or **kargs self.add_bedrooms_per_room = add_bedrooms_per...
_____no_output_____
MIT
sklearn-guide/chapter03/ml-3.ipynb
a630140621/machine-learning-course
特征缩放有两种常见的方法可以让所有的属性有相同的量度:线性函数归一化(Min-Max scaling)和标准化(standardization)。1. 线性函数归一化(许多人称其为归一化(normalization))很简单:值被转变、重新缩放,直到范围变成 0 到 1。我们通过减去最小值,然后再除以最大值与最小值的差值,来进行归一化。> Scikit-Learn 提供了一个转换器 `MinMaxScaler` 来实现这个功能。它有一个超参数 `feature_range`,可以让你改变范围,如果不希望范围是 0 到 1。2. 标准化:首先减去平均值(所以标准化值的平均值总是 0),然后除以方差,使得到的分布具有单位方差。标准化受...
from sklearn.pipeline import Pipeline from sklearn.preprocessing import StandardScaler num_pipeline = Pipeline([ ('imputer', SimpleImputer(strategy="median")), ('attribs_adder', CombinedAttributesAdder()), ('std_scaler', StandardScaler()) ]) housing_num_tr = num_pipeline.fit_transform(housing_num)
_____no_output_____
MIT
sklearn-guide/chapter03/ml-3.ipynb
a630140621/machine-learning-course
现在就有了一个对数值的流水线,还需要对分类值应用 `LabelBinarizer`:如何将这些转换写成一个流水线呢?Scikit-Learn 提供了一个类 `FeatureUnion` 实现这个功能。你给它一列转换器(可以是所有的转换器),当调用它的 `transform()` 方法,每个转换器的 `transform()` 会被 __并行执行__,等待输出,然后将输出合并起来,并返回结果(当然,调用它的 `fit()` 方法就会调用每个转换器的 `fit()`)。
from sklearn.pipeline import FeatureUnion num_attribs = list(housing_num) cat_attribs = ["ocean_proximity"] num_pipeline = Pipeline([ ('selector', DataFrameSelector(num_attribs)), ('imputer', SimpleImputer(strategy="median")), ('attribs_adder', CombinedAttributesAdder()), ('std_scaler', StandardScaler...
_____no_output_____
MIT
sklearn-guide/chapter03/ml-3.ipynb
a630140621/machine-learning-course
选择并训练模型 线性回归
from sklearn.linear_model import LinearRegression lin_reg = LinearRegression() lin_reg.fit(housing_prepared, housing_labels)
_____no_output_____
MIT
sklearn-guide/chapter03/ml-3.ipynb
a630140621/machine-learning-course
完毕!你现在就有了一个可用的线性回归模型。用一些训练集中的实例做下验证:
some_data = housing.iloc[:5] some_labels = housing_labels.iloc[:5] some_data_prepared = full_pipeline.transform(some_data) print("Predictions:\t", lin_reg.predict(some_data_prepared)) print("Labels:\t\t", list(some_labels))
Predictions: [181746.54358872 290558.74963381 244957.50041055 146498.51057872 163230.42389721] Labels: [103000.0, 382100.0, 172600.0, 93400.0, 96500.0]
MIT
sklearn-guide/chapter03/ml-3.ipynb
a630140621/machine-learning-course
RMSE使用 Scikit-Learn 的 `mean_squared_error` 函数,用全部训练集来计算下这个回归模型的 RMSE:
from sklearn.metrics import mean_squared_error housing_predictions = lin_reg.predict(housing_prepared) lin_mse = mean_squared_error(housing_labels, housing_predictions) lin_rmse = np.sqrt(lin_mse) lin_rmse
_____no_output_____
MIT
sklearn-guide/chapter03/ml-3.ipynb
a630140621/machine-learning-course
尝试一个更为复杂的模型。 DecisionTreeRegressor
from sklearn.tree import DecisionTreeRegressor tree_reg = DecisionTreeRegressor() tree_reg.fit(housing_prepared, housing_labels)
_____no_output_____
MIT
sklearn-guide/chapter03/ml-3.ipynb
a630140621/machine-learning-course
RMSE 评估
housing_predictions = tree_reg.predict(housing_prepared) lin_mse = mean_squared_error(housing_labels, housing_predictions) lin_rmse = np.sqrt(lin_mse) lin_rmse
_____no_output_____
MIT
sklearn-guide/chapter03/ml-3.ipynb
a630140621/machine-learning-course
可以发现该模型严重过拟合 交叉验证评估模型的一种方法是用函数 `train_test_split` 来分割训练集,得到一个更小的训练集和一个 __交叉验证集__,然后用更小的训练集来训练模型,用验证集来评估。另一种更好的方法是 __使用 Scikit-Learn 的交叉验证功能__。下面的代码采用了 K 折交叉验证(K-fold cross-validation):它随机地将训练集分成十个不同的子集,成为“折”,然后训练评估决策树模型 10 次,每次选一个不用的折来做评估,用其它 9 个来做训练。结果是一个包含 10 个评分的数组> Scikit-Learn 交叉验证功能期望的是效用函数(越大越好)而不是损失函数(越低越好),因此...
from sklearn.model_selection import cross_val_score scores = cross_val_score(tree_reg, housing_prepared, housing_labels, scoring="neg_mean_squared_error", cv=10) rmse_scores = np.sqrt(-scores) def display_scores(scores): print("Scores:", scores) print("Mean:", scores.mean()) print("Standard deviation:", sco...
Scores: [64669.81202575 70631.54431519 68182.27830444 70392.73509393 72864.28420412 67109.28516943 66338.75100355 69542.07611318 65752.27281003 70391.54164896] Mean: 68587.45806885832 Standard deviation: 2463.4659300283547
MIT
sklearn-guide/chapter03/ml-3.ipynb
a630140621/machine-learning-course
RandomForestRegressor随机森林是通过用特征的随机子集训练许多决策树。在其它多个模型之上建立模型称为集成学习(Ensemble Learning),它是推进 ML 算法的一种好方法。
from sklearn.ensemble import RandomForestRegressor forest_reg = RandomForestRegressor() forest_reg.fit(housing_prepared, housing_labels) scores = cross_val_score(forest_reg, housing_prepared, housing_labels, scoring="neg_mean_squared_error", cv=10, n_jobs=-1) rmse_scores = np.sqrt(-scores) display_scores(rmse_scores)
Scores: [49751.31861666 54615.84913363 52738.25864141 54820.43695375 55833.78571584 49535.30004953 49969.23161663 52868.72231176 51471.9865128 51848.05631902] Mean: 52345.29458710363 Standard deviation: 2125.0902130050936
MIT
sklearn-guide/chapter03/ml-3.ipynb
a630140621/machine-learning-course
保存模型可以使用python自带的 pickle 或 下述函数```pythonfrom sklearn.externals import joblibjoblib.dump(my_model, "my_model.pkl") load my_model_loaded = joblib.load("my_model.pkl")``` 模型微调假设现在有了一个列表,列表里有几个有希望的模型。现在需要对它们进行微调。 网格搜索微调的一种方法是手工调整超参数,直到找到一个好的超参数组合。这么做的话会非常冗长,你也可能没有时间探索多种组合。应该使用 Scikit-Learn 的 `GridSearchCV` 来做这项搜索工作。你所需要...
from sklearn.model_selection import GridSearchCV param_grid = [ {'n_estimators': [3, 10, 30], 'max_features': [2, 4, 6, 8]}, {'bootstrap': [False], 'n_estimators': [3, 10], 'max_features': [2, 3, 4]}, ] forest_reg = RandomForestRegressor() grid_search = GridSearchCV(forest_reg, param_grid, cv=5, scoring='neg...
_____no_output_____
MIT
sklearn-guide/chapter03/ml-3.ipynb
a630140621/machine-learning-course
`param_grid` 告诉 Scikit-Learn 首先评估所有的列在第一个 `dict` 中的 `n_estimators` 和 `max_features` 的 `3 × 4 = 12` 种组合。然后尝试第二个 `dict` 中超参数的 `2 × 3 = 6` 种组合,这次会将超参数 `bootstrap` 设为 `False`。总之,网格搜索会探索 `12 + 6 = 18` 种 `RandomForestRegressor` 的超参数组合,会训练每个模型五次(因为用的是五折交叉验证)。换句话说,训练总共有 `18 × 5 = 90` 轮!K 折将要花费大量时间,完成后,你就能获得参数的最佳组合,如下所示:
grid_search.best_params_ # 参数最佳组合 grid_search.best_estimator_ # 最佳估计器
_____no_output_____
MIT
sklearn-guide/chapter03/ml-3.ipynb
a630140621/machine-learning-course
可以像超参数一样处理数据准备的步骤。例如,__网格搜索可以自动判断是否添加一个你不确定的特征__(比如,使用转换器 `CombinedAttributesAdder` 的超参数 `add_bedrooms_per_room`)。它还能用相似的方法来自动找到处理异常值、缺失特征、特征选择等任务的最佳方法。 随机搜索当探索相对较少的组合时,网格搜索还可以。但是当超参数的搜索空间很大时,最好使用 `RandomizedSearchCV`。这个类的使用方法和类`GridSearchCV` 很相似,但它不是尝试所有可能的组合,而是通过选择每个超参数的一个随机值的特定数量的随机组合。这个方法有两个优点:* 如果你让随机搜索运行,比如 100...
final_model = grid_search.best_estimator_ X_test = test_set.drop("median_house_value", axis=1) y_test = test_set["median_house_value"].copy() # 清洗数据 X_test_prepared = full_pipeline.transform(X_test) # 预测 final_predictions = final_model.predict(X_test_prepared) # RMSE final_mse = mean_squared_error(y_test, final_predic...
_____no_output_____
MIT
sklearn-guide/chapter03/ml-3.ipynb
a630140621/machine-learning-course
Recommending Movies: Retrieval Real-world recommender systems are often composed of two stages:1. The retrieval stage is responsible for selecting an initial set of hundreds of candidates from all possible candidates. The main objective of this model is to efficiently weed out all candidates that the user is not inter...
import os import pprint import tempfile from typing import Dict, Text import numpy as np import tensorflow as tf import tensorflow_datasets as tfds import tensorflow_recommenders as tfrs
_____no_output_____
Apache-2.0
02_usecases/sagemaker_recommendations/wip/02_Recommenders_Retrieval_AdHoc.ipynb
MarcusFra/workshop
Preparing the datasetLet's first have a look at the data.We use the MovieLens dataset from [Tensorflow Datasets](https://www.tensorflow.org/datasets). Loading `movie_lens/100k_ratings` yields a `tf.data.Dataset` object containing the ratings data and loading `movie_lens/100k_movies` yields a `tf.data.Dataset` object c...
# Ratings data. ratings = tfds.load("movie_lens/100k-ratings", split="train") # Features of all the available movies. movies = tfds.load("movie_lens/100k-movies", split="train")
_____no_output_____
Apache-2.0
02_usecases/sagemaker_recommendations/wip/02_Recommenders_Retrieval_AdHoc.ipynb
MarcusFra/workshop
The ratings dataset returns a dictionary of movie id, user id, the assigned rating, timestamp, movie information, and user information:
for x in ratings.take(1).as_numpy_iterator(): pprint.pprint(x)
{'bucketized_user_age': 45.0, 'movie_genres': array([7]), 'movie_id': b'357', 'movie_title': b"One Flew Over the Cuckoo's Nest (1975)", 'raw_user_age': 46.0, 'timestamp': 879024327, 'user_gender': True, 'user_id': b'138', 'user_occupation_label': 4, 'user_occupation_text': b'doctor', 'user_rating': 4.0, 'use...
Apache-2.0
02_usecases/sagemaker_recommendations/wip/02_Recommenders_Retrieval_AdHoc.ipynb
MarcusFra/workshop
The movies dataset contains the movie id, movie title, and data on what genres it belongs to. Note that the genres are encoded with integer labels.
for x in movies.take(1).as_numpy_iterator(): pprint.pprint(x)
{'movie_genres': array([4]), 'movie_id': b'1681', 'movie_title': b'You So Crazy (1994)'}
Apache-2.0
02_usecases/sagemaker_recommendations/wip/02_Recommenders_Retrieval_AdHoc.ipynb
MarcusFra/workshop
In this example, we're going to focus on the ratings data. Other tutorials explore how to use the movie information data as well to improve the model quality.We keep only the `user_id`, and `movie_title` fields in the dataset.
ratings = ratings.map(lambda x: { "movie_title": x["movie_title"], "user_id": x["user_id"], }) movies = movies.map(lambda x: x["movie_title"])
_____no_output_____
Apache-2.0
02_usecases/sagemaker_recommendations/wip/02_Recommenders_Retrieval_AdHoc.ipynb
MarcusFra/workshop
To fit and evaluate the model, we need to split it into a training and evaluation set. In an industrial recommender system, this would most likely be done by time: the data up to time $T$ would be used to predict interactions after $T$.In this simple example, however, let's use a random split, putting 80% of the rating...
tf.random.set_seed(42) shuffled = ratings.shuffle(100_000, seed=42, reshuffle_each_iteration=False) train = shuffled.take(80_000) test = shuffled.skip(80_000).take(20_000)
_____no_output_____
Apache-2.0
02_usecases/sagemaker_recommendations/wip/02_Recommenders_Retrieval_AdHoc.ipynb
MarcusFra/workshop
Let's also figure out unique user ids and movie titles present in the data. This is important because we need to be able to map the raw values of our categorical features to embedding vectors in our models. To do that, we need a vocabulary that maps a raw feature value to an integer in a contiguous range: this allows u...
movie_titles = movies.batch(1_000) user_ids = ratings.batch(1_000_000).map(lambda x: x["user_id"]) unique_movie_titles = np.unique(np.concatenate(list(movie_titles))) unique_user_ids = np.unique(np.concatenate(list(user_ids))) unique_movie_titles[:10]
_____no_output_____
Apache-2.0
02_usecases/sagemaker_recommendations/wip/02_Recommenders_Retrieval_AdHoc.ipynb
MarcusFra/workshop
Implementing a modelChoosing the architecure of our model a key part of modelling.Because we are building a two-tower retrieval model, we can build each tower separately and then combine them in the final model. The query towerLet's start with the query tower.The first step is to decide on the dimensionality of the q...
embedding_dimension = 32
_____no_output_____
Apache-2.0
02_usecases/sagemaker_recommendations/wip/02_Recommenders_Retrieval_AdHoc.ipynb
MarcusFra/workshop
Higher values will correspond to models that may be more accurate, but will also be slower to fit and more prone to overfitting.The second is to define the model itself. Here, we're going to use Keras preprocessing layers to first convert user ids to integers, and then convert those to user embeddings via an `Embedding...
user_model = tf.keras.Sequential([ tf.keras.layers.experimental.preprocessing.StringLookup( vocabulary=unique_user_ids, mask_token=None), # We add an additional embedding to account for unknown tokens. tf.keras.layers.Embedding(len(unique_user_ids) + 1, embedding_dimension) ])
_____no_output_____
Apache-2.0
02_usecases/sagemaker_recommendations/wip/02_Recommenders_Retrieval_AdHoc.ipynb
MarcusFra/workshop
A simple model like this corresponds exactly to a classic [matrix factorization](https://ieeexplore.ieee.org/abstract/document/4781121) approach. While defining a subclass of `tf.keras.Model` for this simple model might be overkill, we can easily extend it to an arbitrarily complex model using standard Keras components...
movie_model = tf.keras.Sequential([ tf.keras.layers.experimental.preprocessing.StringLookup( vocabulary=unique_movie_titles, mask_token=None), tf.keras.layers.Embedding(len(unique_movie_titles) + 1, embedding_dimension) ])
_____no_output_____
Apache-2.0
02_usecases/sagemaker_recommendations/wip/02_Recommenders_Retrieval_AdHoc.ipynb
MarcusFra/workshop
MetricsIn our training data we have positive (user, movie) pairs. To figure out how good our model is, we need to compare the affinity score that the model calculates for this pair to the scores of all the other possible candidates: if the score for the positive pair is higher than for all other candidates, our model ...
metrics = tfrs.metrics.FactorizedTopK( candidates=movies.batch(128).map(movie_model) )
_____no_output_____
Apache-2.0
02_usecases/sagemaker_recommendations/wip/02_Recommenders_Retrieval_AdHoc.ipynb
MarcusFra/workshop
LossThe next component is the loss used to train our model. TFRS has several loss layers and tasks to make this easy.In this instance, we'll make use of the `Retrieval` task object: a convenience wrapper that bundles together the loss function and metric computation:
task = tfrs.tasks.Retrieval( metrics=metrics )
_____no_output_____
Apache-2.0
02_usecases/sagemaker_recommendations/wip/02_Recommenders_Retrieval_AdHoc.ipynb
MarcusFra/workshop
The task itself is a Keras layer that takes the query and candidate embeddings as arguments, and returns the computed loss: we'll use that to implement the model's training loop. The full modelWe can now put it all together into a model. TFRS exposes a base model class (`tfrs.models.Model`) which streamlines bulding m...
class MovielensModel(tfrs.Model): def __init__(self, user_model, movie_model): super().__init__() self.movie_model: tf.keras.Model = movie_model self.user_model: tf.keras.Model = user_model self.task: tf.keras.layers.Layer = task def compute_loss(self, features: Dict[Text, tf.Tensor], training=Fal...
_____no_output_____
Apache-2.0
02_usecases/sagemaker_recommendations/wip/02_Recommenders_Retrieval_AdHoc.ipynb
MarcusFra/workshop
The `tfrs.Model` base class is a simply convenience class: it allows us to compute both training and test losses using the same method.Under the hood, it's still a plain Keras model. You could achieve the same functionality by inheriting from `tf.keras.Model` and overriding the `train_step` and `test_step` functions (s...
class NoBaseClassMovielensModel(tf.keras.Model): def __init__(self, user_model, movie_model): super().__init__() self.movie_model: tf.keras.Model = movie_model self.user_model: tf.keras.Model = user_model self.task: tf.keras.layers.Layer = task def train_step(self, features: Dict[Text, tf.Tensor])...
_____no_output_____
Apache-2.0
02_usecases/sagemaker_recommendations/wip/02_Recommenders_Retrieval_AdHoc.ipynb
MarcusFra/workshop
In these tutorials, however, we stick to using the `tfrs.Model` base class to keep our focus on modelling and abstract away some of the boilerplate. Fitting and evaluatingAfter defining the model, we can use standard Keras fitting and evaluation routines to fit and evaluate the model.Let's first instantiate the model.
model = MovielensModel(user_model, movie_model) model.compile(optimizer=tf.keras.optimizers.Adagrad(learning_rate=0.1))
_____no_output_____
Apache-2.0
02_usecases/sagemaker_recommendations/wip/02_Recommenders_Retrieval_AdHoc.ipynb
MarcusFra/workshop
Then shuffle, batch, and cache the training and evaluation data.
cached_train = train.shuffle(100_000).batch(8192).cache() cached_test = test.batch(4096).cache()
_____no_output_____
Apache-2.0
02_usecases/sagemaker_recommendations/wip/02_Recommenders_Retrieval_AdHoc.ipynb
MarcusFra/workshop
Then train the model:
model.fit(cached_train, epochs=3)
Epoch 1/3 10/10 [==============================] - 5s 464ms/step - factorized_top_k: 0.0508 - factorized_top_k/top_1_categorical_accuracy: 3.2500e-04 - factorized_top_k/top_5_categorical_accuracy: 0.0046 - factorized_top_k/top_10_categorical_accuracy: 0.0117 - factorized_top_k/top_50_categorical_accuracy: 0.0808 - fact...
Apache-2.0
02_usecases/sagemaker_recommendations/wip/02_Recommenders_Retrieval_AdHoc.ipynb
MarcusFra/workshop
As the model trains, the loss is falling and a set of top-k retrieval metrics is updated. These tell us whether the true positive is in the top-k retrieved items from the entire candidate set. For example, a top-5 categorical accuracy metric of 0.2 would tell us that, on average, the true positive is in the top 5 retri...
model.evaluate(cached_test, return_dict=True)
5/5 [==============================] - 1s 169ms/step - factorized_top_k: 0.0782 - factorized_top_k/top_1_categorical_accuracy: 0.0010 - factorized_top_k/top_5_categorical_accuracy: 0.0097 - factorized_top_k/top_10_categorical_accuracy: 0.0226 - factorized_top_k/top_50_categorical_accuracy: 0.1248 - factorized_top_k/top...
Apache-2.0
02_usecases/sagemaker_recommendations/wip/02_Recommenders_Retrieval_AdHoc.ipynb
MarcusFra/workshop
Test set performance is much worse than training performance. This is due to two factors:1. Our model is likely to perform better on the data that it has seen, simply because it can memorize it. This overfitting phenomenon is especially strong when models have many parameters. It can be mediated by model regularization...
# Create a model that takes in raw query features, and index = tfrs.layers.ann.BruteForce(model.user_model) # recommends movies out of the entire movies dataset. index.index(movies.batch(100).map(model.movie_model), movies) # Get recommendations. _, titles = index(tf.constant(["42"])) print(f"Recommendations for user ...
Recommendations for user 42: [b'Bridges of Madison County, The (1995)' b'Father of the Bride Part II (1995)' b'Rudy (1993)']
Apache-2.0
02_usecases/sagemaker_recommendations/wip/02_Recommenders_Retrieval_AdHoc.ipynb
MarcusFra/workshop
Of course, the `BruteForce` layer is going to be too slow to serve a model with many possible candidates. The following sections shows how to speed this up by using an approximate retrieval index. Model servingAfter the model is trained, we need a way to deploy it.In a two-tower retrieval model, serving has two compon...
model_dir = './models' !mkdir $model_dir # Export the query model. path = '{}/query_model'.format(model_dir) model.user_model.save(path) # Load the query model loaded = tf.keras.models.load_model(path, compile=False) query_embedding = loaded(tf.constant(["10"])) print(f"Query embedding: {query_embedding[0, :3]}")
WARNING:tensorflow:11 out of the last 11 calls to <function recreate_function.<locals>.restored_function_body at 0x7f85d75cce18> triggered tf.function retracing. Tracing is expensive and the excessive number of tracings could be due to (1) creating @tf.function repeatedly in a loop, (2) passing tensors with different s...
Apache-2.0
02_usecases/sagemaker_recommendations/wip/02_Recommenders_Retrieval_AdHoc.ipynb
MarcusFra/workshop
Building a candidate ANN indexExporting candidate representations is more involved. Firstly, we want to pre-compute them to make sure serving is fast; this is especially important if the candidate model is computationally intensive (for example, if it has many or wide layers; or uses complex representations for text o...
from annoy import AnnoyIndex index = AnnoyIndex(embedding_dimension, "dot")
_____no_output_____
Apache-2.0
02_usecases/sagemaker_recommendations/wip/02_Recommenders_Retrieval_AdHoc.ipynb
MarcusFra/workshop
Then take the candidate dataset and transform its raw features into embeddings using the movie model:
print(movies) movie_embeddings = movies.enumerate().map(lambda idx, title: (idx, title, model.movie_model(title))) print(movie_embeddings.as_numpy_iterator().next())
(0, b'You So Crazy (1994)', array([ 0.02039416, 0.15982407, 0.0063992 , -0.02597233, 0.12776582, -0.07474077, -0.14477485, -0.03757067, 0.09737739, 0.05545571, 0.06205893, 0.00479794, -0.1288748 , -0.09362403, 0.03417863, -0.03058628, -0.02924258, -0.09905305, -0.08250699, -0.12956885, ...
Apache-2.0
02_usecases/sagemaker_recommendations/wip/02_Recommenders_Retrieval_AdHoc.ipynb
MarcusFra/workshop
And then index the movie_id, movie embedding pairs into our Annoy index:
%%time movie_id_to_title = dict((idx, title) for idx, title, _ in movie_embeddings.as_numpy_iterator()) # We unbatch the dataset because Annoy accepts only scalar (id, embedding) pairs. for movie_id, _, movie_embedding in movie_embeddings.as_numpy_iterator(): index.add_item(movie_id, movie_embedding) # Build a 10-...
_____no_output_____
Apache-2.0
02_usecases/sagemaker_recommendations/wip/02_Recommenders_Retrieval_AdHoc.ipynb
MarcusFra/workshop
We can then retrieve nearest neighbours:
for row in test.batch(1).take(3): query_embedding = model.user_model(row["user_id"])[0] candidates = index.get_nns_by_vector(query_embedding, 3) print(f"User ID: {row['user_id']}, Candidates: {[movie_id_to_title[x] for x in candidates]}.") print(type(candidates))
<class 'list'>
Apache-2.0
02_usecases/sagemaker_recommendations/wip/02_Recommenders_Retrieval_AdHoc.ipynb
MarcusFra/workshop
CLEAN CODE
def is_even(num): if num % 2 == 0: return True elif num % 2 != 0: # We really don't need this condition return False is_even(25) is_even(26) # We will clean up our code above a little bit: def is_even(num): if num % 2 == 0: return True else: return False is_even(12) is_e...
_____no_output_____
Apache-2.0
clean_code.ipynb
MaiaNgo/python-zerotomastery
InstructionsPlease make a copy and rename it with your name (ex: Proj6_Ilmi_Yoon). All grading points should be explored in the notebook but some can be done in a separate pdf file. *Graded questions will be listed with "Q:" followed by the corresponding points.* You will be submitting **a pdf** file containing **the ...
#import python packages import numpy as np import scipy as sp import scipy.stats as st import pandas as pd import seaborn as sns import matplotlib.pyplot as plt rng=np.random.RandomState(1234) #this will ensure the reproducibility of the notebook
_____no_output_____
MIT
Robert_Cacho_Proj2_stats_notebook.ipynb
freshskates/machine-learning
**EXAMPLE I:** ===How much time do subscribers spend on average each day on Netflix?--Example discussed in class (Lecture 1). The data we are working with are simulated, but the mean time spent on Netflix is inspired by https://www.pcmag.com/news/us-netflix-subscribers-watch-32-hours-and-use-96-gb-of-data-per-day (aver...
#Summarizing data #================ population=np.array([1,1.8,2,3.2,3.3,4,4,4.2]) our_sample=np.array([2,3.2,4]) #means population_mean=np.mean(population) print('Population mean',population_mean.round(2)) sample_mean=np.mean(our_sample) print('- Sample mean',sample_mean.round(2)) #standard deviations ...
mean ci_low ci_high 0 2.892338 2.733312 3.051364 1 2.895408 2.729131 3.061684 2 2.946665 2.799507 3.093822 3 2.957376 2.781855 3.132897 4 2.968571 2.784393 3.152748 .. ... ... ... 195 3.427391 3.250704 3.604077 196 3.431677 3.258160 3.605194 197 3.434345 ...
MIT
Robert_Cacho_Proj2_stats_notebook.ipynb
freshskates/machine-learning
**EXAMPLE II:** ===Is exercise associated with lower baseline blood pressure?--We will simulate data with control mean 120 mmHg, treatment mean 116 mmHg and population sd 5 for both conditions.
#simulate dataset #===================== def sample_condition_values(condition_mean, condition_var, condition_N, condition=''): condition_values=np.random.normal(loc = condition_mean, scale=con...
_____no_output_____
MIT
Robert_Cacho_Proj2_stats_notebook.ipynb
freshskates/machine-learning
In our hypothesis test, we ask if these two groups differ significantly from each other. It's a bit hard to say just from looking at the plot. This is where statistics comes in. It's time to:*3. Think about how much the data surprise you, given your null model*We'll convert this step to some math, as follows:**Step 1. ...
mean_ctrl=np.mean(data[data['condition']=='couch']['BP']) mean_test=np.mean(data[data['condition']=='exercise']['BP']) test_stat=mean_test-mean_ctrl print('test statistic =',test_stat)
test statistic = -4.362237456546268
MIT
Robert_Cacho_Proj2_stats_notebook.ipynb
freshskates/machine-learning
What is this number telling us? Is the BP significantly different between the 2 conditions? It's impossible to say looking at only this number.We have to ask ourselves, well, what did you expect?This takes us to the next step. **ii) think about what the test statistic would be if in reality there were no difference bet...
np.random.seed(1) data_exp2=sample_condition_values(condition_mean=ctrl_mean, condition_N=N_per_condition, condition_var=v, condition='control_0') for i in range(1,1001): data_exp2=pd.concat([data_exp2,sample_condition_values(condition_mean=ct...
_____no_output_____
MIT
Robert_Cacho_Proj2_stats_notebook.ipynb
freshskates/machine-learning
In black we have the distribution of test statistics we obtained from the 1000 experiments measuring couch participants. In other words, this is the distribution of the test statistic under the null hypothesis.The red line shows the test statistic from our comparison of exercise group vs with couch group. **Is our diff...
count_more_extreme=int(np.sum(np.abs(null_test_stats)>=np.abs(test_stat))) print(count_more_extreme,'times we got a more extreme test statistic under the null') print(count_more_extreme / 1000,'fraction of the time we got a more extreme test statistic under the null')
3 times we got a more extreme test statistic under the null 0.003 fraction of the time we got a more extreme test statistic under the null
MIT
Robert_Cacho_Proj2_stats_notebook.ipynb
freshskates/machine-learning
What we computed above is called a **p-value**. Now, this is a very often misunderstood term, so let's think about it deeply. Deeply.Deeply.About what it is, what it is not.**P-values**--To remember what a p-value is, you decide to make a promise to me and more importantly yourself, that from now on, any sentence in wh...
from scipy.stats import ttest_ind t_stat,pvalue=ttest_ind(data[data['condition']=='exercise']['BP'], data[data['condition']=='couch']['BP'], ) print(t_stat,pvalue) #as before, compare to the distribution null_test_stats=[] for i in range(1000): current_t,current_pvalue=ttest_ind(d...
10 times we got a more extreme test statistic under the null 0.01 fraction of the time we got a more extreme test statistic under the null = p-value
MIT
Robert_Cacho_Proj2_stats_notebook.ipynb
freshskates/machine-learning
Now, the exciting thing is that we didn't have to perform the second experiment to get an empirical distribution of the test statistic under the null. Rather, we were able to estimate it analytically. And indeed, the p-value we obtained from the t-test is similar to the one we got from our big experiment! Ok, so by now...
#install scanpy !pip install scanpy
Requirement already satisfied: scanpy in c:\users\freshskates\.conda\envs\ml\lib\site-packages (1.8.1) Requirement already satisfied: numpy>=1.17.0 in c:\users\freshskates\.conda\envs\ml\lib\site-packages (from scanpy) (1.20.0) Requirement already satisfied: h5py>=2.10.0 in c:\users\freshskates\.conda\envs\ml\lib\site-...
MIT
Robert_Cacho_Proj2_stats_notebook.ipynb
freshskates/machine-learning
RNA sequencing--RNA sequencing allows us to quantify the extent to which each gene is active in a sample. When a gene is active, its DNA is transcribed into mRNA and then translated into protein. With RNA sequencing, we are counting how frequent mRNAs for each gene occur in a sample. Genes that are more active will hav...
import scanpy as sc def prep_data(): adata=sc.datasets.pbmc3k_processed() counts=pd.DataFrame(np.expm1(adata.raw.X.toarray()), index=adata.raw.obs_names, columns=adata.raw.var_names) #make 3 reps T-cells and 3 reps B-cells cells_per_bulk=100 celltype='CD...
CD4 T cells.rep1 CD4 T cells.rep2 CD4 T cells.rep3 B cells.rep1 \ index MALAT1 8303.0 7334.0 7697.0 5246.0 B2M 4493.0 4675.0 4546.0 2861.0 TMSB4X ...
MIT
Robert_Cacho_Proj2_stats_notebook.ipynb
freshskates/machine-learning
**Let's explore the dataset****(1 pt)** What are the names of the samples?**(2 pts)** What is the highest recorded value? What is the lowest? write code to answer the questions here 1) Sample names are - CD4 T cells.rep1, CD4 T cells.rep2, CD4 T cells.rep3, - B cells.rep1, B cells.rep2, B cells.rep3 2) - The h...
#inspect the data GENE='IL7R' long_data=pd.DataFrame({GENE:data.loc[GENE,:], 'condition':[x.split('.')[0] for x in data.columns]}) print(long_data) sns.catplot(data=long_data,x='condition', y=GENE)
IL7R condition CD4 T cells.rep1 175.0 CD4 T cells CD4 T cells.rep2 128.0 CD4 T cells CD4 T cells.rep3 146.0 CD4 T cells B cells.rep1 13.0 B cells B cells.rep2 10.0 B cells B cells.rep3 20.0 B cells
MIT
Robert_Cacho_Proj2_stats_notebook.ipynb
freshskates/machine-learning
**Two-sample t-test for one gene across 2 conditions** We are now going to check whether the gene IL7R is differentially active in CD4 T cells vs B cells. **(1 pt)** What is the null hypothesis? **(1 pt)** Based on your plot of the gene in the two conditions, and the fact that there looks like there might be a dif...
#pick 1 gene, do 1 t-test GENE='IL7R' COND1=['CD4 T cells.rep' + str(x+1) for x in range(3)] COND2=['B cells.rep' + str(x+1) for x in range(3)] #plot gene across samples #t-test from scipy.stats import ttest_ind t_stat,pvalue=ttest_ind(data.loc[GENE,COND1],data.loc[GENE,COND2]) print('t statistic',t_stat.ro...
t statistic 9.66 p-value 0.00064
MIT
Robert_Cacho_Proj2_stats_notebook.ipynb
freshskates/machine-learning
**Two-sample t-tests for each gene across 2 conditions**We are now going to repeat our analysis from before for all genes in our dataset.**(1 pt)** How many genes are present in our dataset? Answers 11) - 13714 genes present in the dataset, displayed with display(results)
from IPython.display import display #all genes t-tests PSEUDOCOUNT=1 results=pd.DataFrame(index=data.index, columns=['t','p','lfc']) for gene in data.index: t_stat,pvalue=ttest_ind(data.loc[gene,COND1],data.loc[gene,COND2]) lfc=np.log2((data.loc[gene,COND1].mean()+PSEUDOCOUNT)/(data.lo...
_____no_output_____
MIT
Robert_Cacho_Proj2_stats_notebook.ipynb
freshskates/machine-learning
**Ranking discoveries by either significance or fold change**For each gene, we have obtained:- a t-statistic- a p-value for the difference between the 2 conditions- a log2 fold change between CD4 T cells and B cellsWe can inspect how fold changes relate to the significance of the differences. **(1 pt)** What do you exp...
#volcano plot ###### results['p']=results['p'].fillna(1) PS2=1e-7 plt.scatter(results['lfc'],-np.log10(results['p']+PS2),s=5,alpha=0.5,color='black') plt.xlabel('Log2 fold change (CD4 T cells/B cells)') plt.ylabel('-log10(p-value)') plt.show() display(results)
_____no_output_____
MIT
Robert_Cacho_Proj2_stats_notebook.ipynb
freshskates/machine-learning
**Multiple testing correction**Now, we will explore how the number of differentially active genes differs depending on how we correct for multiple tests.**(1 pt)** How many genes pass the significance level of 0.05, without performing any correction for multiple testing? Answers 13) - there are 1607 genes that pass th...
ALPHA=0.05 print((results['p']<=ALPHA).sum())
1607
MIT
Robert_Cacho_Proj2_stats_notebook.ipynb
freshskates/machine-learning
We will use a function that adjusts our p-values using different methods, called "multipletests". You can read about it here: https://www.statsmodels.org/dev/generated/statsmodels.stats.multitest.multipletests.htmlWe will use the following settings:- for Bonferroni correction, we set method='bonferroni'. This will mult...
#multiple testing correction #bonferroni from statsmodels.stats.multitest import multipletests results['p.adj.bonferroni']=multipletests(results['p'], method='bonferroni')[1] FDR=ALPHA plt.hist(results['p'],100) plt.axvline(x=FDR,color='red',linestyle='--') plt.xlabel('Unadjusted p-values') plt.ylabel('Num...
_____no_output_____
MIT
Robert_Cacho_Proj2_stats_notebook.ipynb
freshskates/machine-learning
**(1 pt)** How many genes pass the significance level of 0.05, after correcting for multiple testing using the Benjamini-Hochberg method? Answers 16) - 220
results['p.adj.bh']=multipletests(results['p'], method='fdr_bh')[1] FDR=0.05 plt.hist(results['p'],100) plt.axvline(x=FDR,color='red',linestyle='--') plt.xlabel('Unadjusted p-values') plt.ylabel('Number of genes') plt.show() plt.hist(results['p.adj.bh'],100) plt.ylim(0,2000) plt.axvline(x=FDR,color='red',lin...
_____no_output_____
MIT
Robert_Cacho_Proj2_stats_notebook.ipynb
freshskates/machine-learning
**(1 pt)** Which multiple testing correction is the most stringent? Finally, let's look at our results. Print the significant differential genes and look up a few on the internet. Answers 17) - Bonferroni, and this is because the corrected p values resulted in values of 1 or greater, so it was limited to 1
results.loc[results['p.adj.bonferroni']<=FDR,:].sort_values(by='lfc')
_____no_output_____
MIT
Robert_Cacho_Proj2_stats_notebook.ipynb
freshskates/machine-learning
Copyright 2018 The TF-Agents Authors.
#@title Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # https://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under...
_____no_output_____
Apache-2.0
docs/tutorials/6_reinforce_tutorial.ipynb
Zuu97/agents
REINFORCE agent View on TensorFlow.org Run in Google Colab View source on GitHub Download notebook Introduction This example shows how to train a [REINFORCE](http://www-anw.cs.umass.edu/~barto/courses/cs687/williams92simple.pdf) agent on the Cartpole environment usi...
!sudo apt-get install -y xvfb ffmpeg !pip install gym !pip install 'imageio==2.4.0' !pip install PILLOW !pip install 'pyglet==1.3.2' !pip install pyvirtualdisplay !pip install tf-agents from __future__ import absolute_import from __future__ import division from __future__ import print_function import base64 import ima...
_____no_output_____
Apache-2.0
docs/tutorials/6_reinforce_tutorial.ipynb
Zuu97/agents
Hyperparameters
env_name = "CartPole-v0" # @param {type:"string"} num_iterations = 250 # @param {type:"integer"} collect_episodes_per_iteration = 2 # @param {type:"integer"} replay_buffer_capacity = 2000 # @param {type:"integer"} fc_layer_params = (100,) learning_rate = 1e-3 # @param {type:"number"} log_interval = 25 # @param {type:...
_____no_output_____
Apache-2.0
docs/tutorials/6_reinforce_tutorial.ipynb
Zuu97/agents
EnvironmentEnvironments in RL represent the task or problem that we are trying to solve. Standard environments can be easily created in TF-Agents using `suites`. We have different `suites` for loading environments from sources such as the OpenAI Gym, Atari, DM Control, etc., given a string environment name.Now let us ...
env = suite_gym.load(env_name)
_____no_output_____
Apache-2.0
docs/tutorials/6_reinforce_tutorial.ipynb
Zuu97/agents
We can render this environment to see how it looks. A free-swinging pole is attached to a cart. The goal is to move the cart right or left in order to keep the pole pointing up.
#@test {"skip": true} env.reset() PIL.Image.fromarray(env.render())
_____no_output_____
Apache-2.0
docs/tutorials/6_reinforce_tutorial.ipynb
Zuu97/agents
The `time_step = environment.step(action)` statement takes `action` in the environment. The `TimeStep` tuple returned contains the environment's next observation and reward for that action. The `time_step_spec()` and `action_spec()` methods in the environment return the specifications (types, shapes, bounds) of the `t...
print('Observation Spec:') print(env.time_step_spec().observation) print('Action Spec:') print(env.action_spec())
_____no_output_____
Apache-2.0
docs/tutorials/6_reinforce_tutorial.ipynb
Zuu97/agents
So, we see that observation is an array of 4 floats: the position and velocity of the cart, and the angular position and velocity of the pole. Since only two actions are possible (move left or move right), the `action_spec` is a scalar where 0 means "move left" and 1 means "move right."
time_step = env.reset() print('Time step:') print(time_step) action = np.array(1, dtype=np.int32) next_time_step = env.step(action) print('Next time step:') print(next_time_step)
_____no_output_____
Apache-2.0
docs/tutorials/6_reinforce_tutorial.ipynb
Zuu97/agents
Usually we create two environments: one for training and one for evaluation. Most environments are written in pure python, but they can be easily converted to TensorFlow using the `TFPyEnvironment` wrapper. The original environment's API uses numpy arrays, the `TFPyEnvironment` converts these to/from `Tensors` for you ...
train_py_env = suite_gym.load(env_name) eval_py_env = suite_gym.load(env_name) train_env = tf_py_environment.TFPyEnvironment(train_py_env) eval_env = tf_py_environment.TFPyEnvironment(eval_py_env)
_____no_output_____
Apache-2.0
docs/tutorials/6_reinforce_tutorial.ipynb
Zuu97/agents
AgentThe algorithm that we use to solve an RL problem is represented as an `Agent`. In addition to the REINFORCE agent, TF-Agents provides standard implementations of a variety of `Agents` such as [DQN](https://storage.googleapis.com/deepmind-media/dqn/DQNNaturePaper.pdf), [DDPG](https://arxiv.org/pdf/1509.02971.pdf),...
actor_net = actor_distribution_network.ActorDistributionNetwork( train_env.observation_spec(), train_env.action_spec(), fc_layer_params=fc_layer_params)
_____no_output_____
Apache-2.0
docs/tutorials/6_reinforce_tutorial.ipynb
Zuu97/agents
We also need an `optimizer` to train the network we just created, and a `train_step_counter` variable to keep track of how many times the network was updated.
optimizer = tf.compat.v1.train.AdamOptimizer(learning_rate=learning_rate) train_step_counter = tf.compat.v2.Variable(0) tf_agent = reinforce_agent.ReinforceAgent( train_env.time_step_spec(), train_env.action_spec(), actor_network=actor_net, optimizer=optimizer, normalize_returns=True, train_st...
_____no_output_____
Apache-2.0
docs/tutorials/6_reinforce_tutorial.ipynb
Zuu97/agents
PoliciesIn TF-Agents, policies represent the standard notion of policies in RL: given a `time_step` produce an action or a distribution over actions. The main method is `policy_step = policy.step(time_step)` where `policy_step` is a named tuple `PolicyStep(action, state, info)`. The `policy_step.action` is the `actio...
eval_policy = tf_agent.policy collect_policy = tf_agent.collect_policy
_____no_output_____
Apache-2.0
docs/tutorials/6_reinforce_tutorial.ipynb
Zuu97/agents
Metrics and EvaluationThe most common metric used to evaluate a policy is the average return. The return is the sum of rewards obtained while running a policy in an environment for an episode, and we usually average this over a few episodes. We can compute the average return metric as follows.
#@test {"skip": true} def compute_avg_return(environment, policy, num_episodes=10): total_return = 0.0 for _ in range(num_episodes): time_step = environment.reset() episode_return = 0.0 while not time_step.is_last(): action_step = policy.action(time_step) time_step = environment.step(acti...
_____no_output_____
Apache-2.0
docs/tutorials/6_reinforce_tutorial.ipynb
Zuu97/agents
Replay BufferIn order to keep track of the data collected from the environment, we will use the TFUniformReplayBuffer. This replay buffer is constructed using specs describing the tensors that are to be stored, which can be obtained from the agent using `tf_agent.collect_data_spec`.
replay_buffer = tf_uniform_replay_buffer.TFUniformReplayBuffer( data_spec=tf_agent.collect_data_spec, batch_size=train_env.batch_size, max_length=replay_buffer_capacity)
_____no_output_____
Apache-2.0
docs/tutorials/6_reinforce_tutorial.ipynb
Zuu97/agents
For most agents, the `collect_data_spec` is a `Trajectory` named tuple containing the observation, action, reward etc. Data CollectionAs REINFORCE learns from whole episodes, we define a function to collect an episode using the given data collection policy and save the data (observations, actions, rewards etc.) as tra...
#@test {"skip": true} def collect_episode(environment, policy, num_episodes): episode_counter = 0 environment.reset() while episode_counter < num_episodes: time_step = environment.current_time_step() action_step = policy.action(time_step) next_time_step = environment.step(action_step.action) tr...
_____no_output_____
Apache-2.0
docs/tutorials/6_reinforce_tutorial.ipynb
Zuu97/agents
Training the agentThe training loop involves both collecting data from the environment and optimizing the agent's networks. Along the way, we will occasionally evaluate the agent's policy to see how we are doing.The following will take ~3 minutes to run.
#@test {"skip": true} try: %%time except: pass # (Optional) Optimize by wrapping some of the code in a graph using TF function. tf_agent.train = common.function(tf_agent.train) # Reset the train step tf_agent.train_step_counter.assign(0) # Evaluate the agent's policy once before training. avg_return = compute_av...
_____no_output_____
Apache-2.0
docs/tutorials/6_reinforce_tutorial.ipynb
Zuu97/agents
Visualization PlotsWe can plot return vs global steps to see the performance of our agent. In `Cartpole-v0`, the environment gives a reward of +1 for every time step the pole stays up, and since the maximum number of steps is 200, the maximum possible return is also 200.
#@test {"skip": true} steps = range(0, num_iterations + 1, eval_interval) plt.plot(steps, returns) plt.ylabel('Average Return') plt.xlabel('Step') plt.ylim(top=250)
_____no_output_____
Apache-2.0
docs/tutorials/6_reinforce_tutorial.ipynb
Zuu97/agents
Videos It is helpful to visualize the performance of an agent by rendering the environment at each step. Before we do that, let us first create a function to embed videos in this colab.
def embed_mp4(filename): """Embeds an mp4 file in the notebook.""" video = open(filename,'rb').read() b64 = base64.b64encode(video) tag = ''' <video width="640" height="480" controls> <source src="data:video/mp4;base64,{0}" type="video/mp4"> Your browser does not support the video tag. </video>'''.for...
_____no_output_____
Apache-2.0
docs/tutorials/6_reinforce_tutorial.ipynb
Zuu97/agents
The following code visualizes the agent's policy for a few episodes:
num_episodes = 3 video_filename = 'imageio.mp4' with imageio.get_writer(video_filename, fps=60) as video: for _ in range(num_episodes): time_step = eval_env.reset() video.append_data(eval_py_env.render()) while not time_step.is_last(): action_step = tf_agent.policy.action(time_step) time_step ...
_____no_output_____
Apache-2.0
docs/tutorials/6_reinforce_tutorial.ipynb
Zuu97/agents
Querying Nexus knowledge graph using SPARQLThe goal of this notebook is to learn the basics of SPARQL. Only the READ part of SPARQL will be exposed. PrerequisitesThis notebook assumes you've created a project within the AWS deployment of Nexus. If not follow the Blue Brain Nexus [Quick Start tutorial](https://bluebra...
#Configuration for the Nexus deployment nexus_deployment = "https://nexus-sandbox.io/v1" token= "your token here" org ="tutorialnexus" project ="$PROJECTLABEL" headers = {} #Let install sparqlwrapper which a python wrapper around sparql client !pip install git+https://github.com/RDFLib/sparqlwrapper # Utility functi...
_____no_output_____
Apache-2.0
src/main/paradox/docs/tutorial/notebooks/Query_Sparql_View.ipynb
clifle/nexus
Step 2: Explore and navigate data using the SPARQL query language Let write our first query.
select_all_query = """ SELECT ?s ?p ?o WHERE { ?s ?p ?o } OFFSET 0 LIMIT 5 """ nexus_results = query_sparql(select_all_query,sparqlview_wrapper) nexus_df =sparql2dataframe(nexus_results) nexus_df.head()
_____no_output_____
Apache-2.0
src/main/paradox/docs/tutorial/notebooks/Query_Sparql_View.ipynb
clifle/nexus
Most SPARQL queries you'll see will have the anotomy above with:* a **SELECT** clause that let you select the variables you want to retrieve* a **WHERE** clause defining a set of constraints that the variables should satisfy to be retrieved* **LIMIT** and **OFFSET** clauses to enable pagination* the constraints are usu...
movie_with_title = """ PREFIX vocab: <https://nexus-sandbox.io/v1/vocabs/%s/%s/> PREFIX nxv: <https://bluebrain.github.io/nexus/vocabulary/> Select ?movie ?title WHERE { ?movie a vocab:Movie. ?movie vocab:title ?title. } LIMIT 5 """%(org,project) nexus_results = query_sparql(movie_with_title,sparqlview_wrapp...
_____no_output_____
Apache-2.0
src/main/paradox/docs/tutorial/notebooks/Query_Sparql_View.ipynb
clifle/nexus
Note PREFIX clauses. It is way to shorten URIS within a SPARQL query. Without them we would have to use full URI for all properties.The ?movie variable is bound to a URI (the internal Nexus id). Let retrieve the movieId just like in the MovieLens csv files for simplicity.
movie_with_title = """ PREFIX vocab: <https://nexus-sandbox.io/v1/vocabs/%s/%s/> PREFIX nxv: <https://bluebrain.github.io/nexus/vocabulary/> Select ?movieId ?title WHERE { # Select movies ?movie a vocab:Movie. # Select their movieId value ?movie vocab:movieId ?movieId. # ?movie voca...
_____no_output_____
Apache-2.0
src/main/paradox/docs/tutorial/notebooks/Query_Sparql_View.ipynb
clifle/nexus
In the above query movies are things (or entities) of type vocab:Movie. This is a typical instance query where entities are filtered by their type(s) and then some of their properties are retrieved (here ?title). Let retrieve everything that is linked (outgoing) to the movies. The * character in the SELECT clause indic...
movie_with_properties = """ PREFIX vocab: <https://nexus-sandbox.io/v1/vocabs/%s/%s/> PREFIX nxv: <https://bluebrain.github.io/nexus/vocabulary/> Select * WHERE { ?movie a vocab:Movie. ?movie ?p ?o. } LIMIT 20 """%(org,project) nexus_results = query_sparql(movie_with_properties,sparqlview_wrapper) nexus_df ...
_____no_output_____
Apache-2.0
src/main/paradox/docs/tutorial/notebooks/Query_Sparql_View.ipynb
clifle/nexus
As a little exercise, write a query retrieving incoming entities to movies. You can copy past the query above and modify it.Hints: ?s ?p ?o can be read as: ?o is linked to ?s with an outgoing link.Do you have results ?
#Your query here
_____no_output_____
Apache-2.0
src/main/paradox/docs/tutorial/notebooks/Query_Sparql_View.ipynb
clifle/nexus
Let retrieve the movie ratings
movie_with_properties = """ PREFIX vocab: <https://nexus-sandbox.io/v1/vocabs/%s/%s/> PREFIX nxv: <https://bluebrain.github.io/nexus/vocabulary/> Select ?userId ?movieId ?rating ?timestamp WHERE { ?movie a vocab:Movie. ?movie vocab:movieId ?movieId. ?ratingNode vocab:movieId ?ratingmovieId. ...
_____no_output_____
Apache-2.0
src/main/paradox/docs/tutorial/notebooks/Query_Sparql_View.ipynb
clifle/nexus
As a little exercise, write a query retrieving the movie tags along with the user id and timestamp. You can copy and past the query above and modify it.
#Your query here
_____no_output_____
Apache-2.0
src/main/paradox/docs/tutorial/notebooks/Query_Sparql_View.ipynb
clifle/nexus
Aggregate queries [Aggregates](https://www.w3.org/TR/sparql11-query/aggregates) apply some operations over a group of solutions.Available aggregates are: COUNT, SUM, MIN, MAX, AVG, GROUP_CONCAT, and SAMPLE.We will not see them all but we'll look at some examples. The next query will compute the average rating score fo...
tag_value = "funny" movie_avg_ratings = """ PREFIX vocab: <https://nexus-sandbox.io/v1/vocabs/%s/%s/> PREFIX nxv: <https://bluebrain.github.io/nexus/vocabulary/> Select ( AVG(?ratingvalue) AS ?score) WHERE { # Select movies ?movie a vocab:Movie. # Select their movieId value ?movie vocab:movieId ?mov...
_____no_output_____
Apache-2.0
src/main/paradox/docs/tutorial/notebooks/Query_Sparql_View.ipynb
clifle/nexus
Retrieve the number of tags per movie. Can be a little bit slow depending on the size of your data.
nbr_tags_per_movie = """ PREFIX vocab: <https://nexus-sandbox.io/v1/vocabs/%s/%s/> PREFIX nxv: <https://bluebrain.github.io/nexus/vocabulary/> Select ?title (COUNT(?tagvalue) as ?tagnumber) WHERE { # Select movies ?movie a vocab:Movie. # Select their movieId value ?movie vocab:movieId ?movieId. ...
_____no_output_____
Apache-2.0
src/main/paradox/docs/tutorial/notebooks/Query_Sparql_View.ipynb
clifle/nexus
The next query will retrieve movies along with users that tagged them separated by a comma
# Group Concat movie_tag_users = """ PREFIX vocab: <https://nexus-sandbox.io/v1/vocabs/%s/%s/> PREFIX nxv: <https://bluebrain.github.io/nexus/vocabulary/> Select ?movieId (group_concat(DISTINCT ?userId;separator=",") as ?users) WHERE { # Select movies ?movie a vocab:Movie. # Select their movieId value ...
_____no_output_____
Apache-2.0
src/main/paradox/docs/tutorial/notebooks/Query_Sparql_View.ipynb
clifle/nexus
Model Data Prep
df_log = val_data.copy() probas_cols = ["fla_" + str(i) for i in range(1,28)] + ["cam_" + str(i) for i in range(1,28)] +\ ["res_" + str(i) for i in range(1,28)] \ + ["vca_" + str(i) for i in range(1,28)] \ X = df_log[probas_cols] y = df_log['labels'].values X_train, X_test, y_train, y_test = train_test_split(X, y, st...
_____no_output_____
MIT
Boosted Late-Fusion.ipynb
Sakina8/Multimodal-Classification2020
Model Training
### Run lightgbm to get weights for different class logits t0 = time.time() model_met = 'fit' #'xgb'#'train' #fit params = { "objective" : "multiclass", "num_class" : num_class, "num_leaves" : 60, "max_depth": -1, "learning_rate" : 0.01, "bagging_fraction" ...
_____no_output_____
MIT
Boosted Late-Fusion.ipynb
Sakina8/Multimodal-Classification2020
Example usage of the O-C tools This example shows how to construct and fit with MCMC the O-C diagram of the RR Lyrae star OGLE-BLG-RRLYR-02950 We start with importing some libraries
import numpy as np import oc_tools as octs
_____no_output_____
MIT
06498_oc.ipynb
gerhajdu/rrl_binaries_1
We read in the data, set the period used to construct the O-C diagram (and to fold the light curve to construct the template curves, etc.), and the orders of the Fourier series we will fit to the light curve in the first and second iterations in the process
who = "06498" period = 0.589490 order1 = 10 order2 = 15 jd3, mag3 = np.loadtxt('data/{:s}.o3'.format(who), usecols=[0,1], unpack=True) jd4, mag4 = np.loadtxt('data/{:s}.o4'.format(who), usecols=[0,1], unpack=True)
_____no_output_____
MIT
06498_oc.ipynb
gerhajdu/rrl_binaries_1
We correct for possible average magnitude and amplitude differences between The OGLE-III and IV photometries by moving the intensity average of the former to the intensity average measured for the latter The variables "jd" and "mag" contain the merged timings and magnitudes of the OGLE-III + IV photometry, wich are us...
mag3_shift=octs.shift_int(jd3, mag3, jd4, mag4, order1, period, plot=True) jd = np.hstack((jd3,jd4)) mag = np.hstack((mag3_shift, mag4))
_____no_output_____
MIT
06498_oc.ipynb
gerhajdu/rrl_binaries_1
Calling the split_lc_seasons() function provides us with an array containing masks splitting the combined light curve into short sections, depending on the number of points Optionally, the default splitting can be overriden by using the optional parameters "limits" and "into". For example, calling the function as:octs...
splits = octs.split_lc_seasons(jd, plot=True, mag = mag)
_____no_output_____
MIT
06498_oc.ipynb
gerhajdu/rrl_binaries_1
The function calc_oc_points() fits the light curve of the variable to produce a template, and uses it to determine the O-C points of the individual segments
oc_jd, oc_oc = octs.calc_oc_points(jd, mag, period, order1, splits, figure=True)
_____no_output_____
MIT
06498_oc.ipynb
gerhajdu/rrl_binaries_1
We make a guess at the binary parameters
e = 0.37 P_orb = 2800. T_peri = 6040 a_sini = 0.011 omega = -0.7 a= -8e-03 b= 3e-06 c= -3.5e-10 params = np.asarray((e, P_orb, T_peri, a_sini, omega, a, b, c)) lower_bounds = np.array((0., 100., -np.inf, 0.0, -np.inf, -np.inf, -np.inf, -np.inf)) upper_bounds = np.array((0.99, 6000., np.inf, 1.0, np.inf, ...
_____no_output_____
MIT
06498_oc.ipynb
gerhajdu/rrl_binaries_1
We use the above guesses as the starting point (dashed grey line on the plot below) to find the O-C LTTE solution of the first iteration of our procedure. The yellow line on the plot shows the fit. The vertical blue bar shows the timing of the periastron passage Note that in this function also provides the timings of ...
params2, jd2 = octs.fit_oc1(oc_jd, oc_oc, jd, params, lower_bounds, upper_bounds)
_____no_output_____
MIT
06498_oc.ipynb
gerhajdu/rrl_binaries_1
We use the initial solution as the starting point for the MCMC fit, therefore we prepare it first by transforming $e$ and $\omega$ to $\sqrt{e}\sin{\omega}$ and $\sqrt{e}\sin{\omega}$ For each parameter, we also have a lower and higher limit in its prior, but the values given for $\sqrt{e}\sin{\omega}$ and $\sqrt{e}\s...
start = np.zeros_like(params2) start[0:3] = params2[1:4] start[3] = np.sqrt(params2[0]) * np.sin(params2[4]) start[4] = np.sqrt(params2[0]) * np.cos(params2[4]) start[5:] = params2[5:] prior_ranges = np.asanyarray([[start[0]*0.9, start[0]*1.1], [start[1]-start[0]/4., sta...
_____no_output_____
MIT
06498_oc.ipynb
gerhajdu/rrl_binaries_1
We set a random seed to get reproducible results, then prepare the initial positions of the 200 walkers we are using during the fitting. During this, we check explicitly that these correspond to a position with a finite prior (i.e., they are not outside of the prior ranges defined above)
np.random.seed(0) walkers = 200 random_scales = np.array((1e+1, 1e+1, 1e-4, 1e-2, 1e-2, 1e-3, 2e-7, 5e-11)) pos = np.zeros((walkers, start.size)) for i in range(walkers): pos[i,:] = start + random_scales * np.random.normal(size=8) while np.isinf(octs.log_prior(pos[i,:], prior_ranges)): pos[i,:] = s...
_____no_output_____
MIT
06498_oc.ipynb
gerhajdu/rrl_binaries_1
We recalculate the O-C points, but this time we use a higher-order Fourier series to fit the light curve with the modified timings, and we also calculate errors using bootstrapping
oc_jd, oc_oc, oc_sd = octs.calc_oc_points(jd, mag, period, order2, splits, bootstrap_times = 500, jd_mod = jd2, figure=True)
_____no_output_____
MIT
06498_oc.ipynb
gerhajdu/rrl_binaries_1