Install tensorflow on MacBook Pro M1 and kerns for R

I have a 2021 MacBook Pro with Apple M1 Pro chip. I tried to install tensor flow on it following the instruction of Apple here https://developer.apple.com/metal/tensorflow-plugin/ as well as many other instructions online but failed to make it work.

Install tensorflow

After trial and error, this is what worked for me.

  1. Follow the instruction here https://developer.apple.com/metal/tensorflow-plugin/ to install miniconda.
  2. Install tensorflow using python -m pip install tensorflow-macos==2.9
  3. Install tensorflow-metal using python -m pip install tensorflow-metal==0.5.0

After this, you can test the installation using the example below. First, save the following code to a file such as test.py.

import tensorflow as tf
import ssl
ssl._create_default_https_context = ssl._create_unverified_context

cifar = tf.keras.datasets.cifar100
(x_train, y_train), (x_test, y_test) = cifar.load_data()
model = tf.keras.applications.ResNet50(
    include_top=True,
    weights=None,
    input_shape=(32, 32, 3),
    classes=100,)

loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
model.compile(optimizer="adam", loss=loss_fn, metrics=["accuracy"])
model.fit(x_train, y_train, epochs=5, batch_size=64)

Then, test it using python test.py.

Install keras for R

To use tensorflow in R, first install the packages tensorflow and keras within R using

install.packages('tensorflow')
install.packages('keras')

Then, create or open the .Renviron file within your user directory, e.g., using nano ~/.Renviron. Add the following in the file (this will allow R to find your python and tensorflow installation.

RETICULATE_PYTHON = "~/miniconda/bin/python"

Now, you can test whether it works or not within R using the example below (from the book Introduction to Machine Learning).

## Data
library(ISLR2)  ## install the package if not yet
Gitters <- na.omit(Hitters) 
n <- nrow(Gitters)
set.seed(13)
ntest <- trunc(n / 3)
testid <- sample(1:n, ntest)
x <- scale(model.matrix(Salary ~ . - 1, data = Gitters)) 
y <- Gitters$Salary

## Now run a simple example
library(keras)

## this is used to build the model
## first layer is the hidden layer with 50 nodes
## output one layer with one node active the linear activiation function
modnn <- keras_model_sequential() %>%
    layer_dense(units = 50, activation = "relu", input_shape = ncol(x)) %>%
    layer_dropout(rate = 0.4) %>% 
    layer_dense(units = 1, activation = "linear")

summary(modnn)
## communicate with python

modnn %>% compile(loss = "mse", 
                  optimizer = optimizer_rmsprop(),
                  metrics = list("mean_absolute_error") )


## conduct the analysis
nn.fit.1layer <- modnn %>% fit(
    x[-testid, ], y[-testid], 
    epochs = 1000, batch_size = 32, 
    validation_data = list(x[testid, ], y[testid])
)
2023/01/06 20:07 · johnny · 0 Comments · 0 Linkbacks

Hugo and PHP for building Websites

I built almost all my websites using PHP. Recently, I started to build simple HTML sites using Hugo, which was amazing. However, occasionally, I missed the flexibility of PHP for server side processing. I have tried to output PHP files instead of html files from Hugo but was not successful. Eventually, I resolved to make the change to the web server instead. Basically, one can change the setting of the server to parse .html files as php files. Several ways can be used. For example, one can use .htaccess with the following

AddType application/x-httpd-php .html

One can also change the configure file to add the option for the directory.

Now, I can use Hugo together with PHP.

2021/06/19 18:06 · johnny · 0 Comments · 0 Linkbacks

Flatten comments in Acrobat Pro

Bring up the Preflight tool (search preflight), then use the “Flatten annotations and field” in the “PDF Fixups” (search flatten) category.

2019/09/24 10:51 · johnny · 0 Comments · 0 Linkbacks

Quantify the impact of your R packages

Software development often takes a lot of time but its impact is often difficult to evaluate. If you have developed an R package and upload it to CRAN, you might be able to get some download information but not a lot.

Since R studio hosts the CRAN mirror, it keeps tracking the download statistics of the R packages installed from the RStudio mirror and therefore provides some useful information. It also provides an R package -cranlogs- to get the information within R. Certainly, the information is not incomplete but at least it provides the developers some ideas on how their packages are used.

For example, the code below is used to get the download information in the past month of an R package coefficientalpha I developed.

library(cranlogs)

cran_downloads('coefficientalpha', when='last-month')

         date count          package
1  2019-04-08    15 coefficientalpha
2  2019-04-09    15 coefficientalpha
3  2019-04-10    25 coefficientalpha
4  2019-04-11    18 coefficientalpha
5  2019-04-12    25 coefficientalpha
6  2019-04-13    13 coefficientalpha
7  2019-04-14    11 coefficientalpha
8  2019-04-15    22 coefficientalpha
9  2019-04-16    15 coefficientalpha
10 2019-04-17    31 coefficientalpha
11 2019-04-18    11 coefficientalpha
12 2019-04-19    19 coefficientalpha
13 2019-04-20    14 coefficientalpha
14 2019-04-21    14 coefficientalpha
15 2019-04-22    19 coefficientalpha
16 2019-04-23     6 coefficientalpha
17 2019-04-24    21 coefficientalpha
18 2019-04-25    19 coefficientalpha
19 2019-04-26     7 coefficientalpha
20 2019-04-27    11 coefficientalpha
21 2019-04-28    13 coefficientalpha
22 2019-04-29    38 coefficientalpha
23 2019-04-30    14 coefficientalpha
24 2019-05-01    18 coefficientalpha
25 2019-05-02    18 coefficientalpha
26 2019-05-03    10 coefficientalpha
27 2019-05-04     3 coefficientalpha
28 2019-05-05    15 coefficientalpha
29 2019-05-06    58 coefficientalpha
30 2019-05-07    39 coefficientalpha
2019/05/08 15:55 · johnny · 0 Comments · 0 Linkbacks

Apache log process using GoAccess

sudo zcat /var/log/apache2/access-advstats.*.gz | sudo goaccess /var/log/apache2/access-advstats.log -o report.html --log-format=COMBINED
2019/03/04 09:41 · johnny · 0 Comments · 0 Linkbacks

Older entries >>


Note. Everything on this blog only reflects my personal view which may or may not be true and is not related to any organization or institute.